Repository: mars-project/mars

Issue:
[BUG][TENSOR] mt.random.rand returns different results when executed in different sessions
**Describe the bug**

A random tensor returns different results when executed in different sessions. 

**To Reproduce**

```python
In [2]: import mars.tensor as mt                                                

In [3]: a = mt.random.rand(10, 20)                                              

In [4]: from mars.session import new_session                                    

In [5]: session = new_session()                                                 

In [6]: session.run(a)                                                          
Out[6]: 
array([[0.40063535, 0.45796697, 0.36712569, 0.57410564, 0.03316188,
        0.39214044, 0.58855336, 0.04936224, 0.62870674, 0.89812263,
        0.0950565 , 0.94972637, 0.01124284, 0.79766443, 0.53592006,
        0.63557538, 0.25438726, 0.95038912, 0.26640497, 0.94912579],
       [0.31029473, 0.47018138, 0.93268746, 0.24141035, 0.36309559,
        0.74774755, 0.83962496, 0.69642224, 0.66627376, 0.83162516,
        0.06235467, 0.95071957, 0.67219839, 0.54850925, 0.8226961 ,
        0.88564641, 0.78807562, 0.94937921, 0.73962081, 0.57448563],
       [0.92392073, 0.25436821, 0.46914224, 0.00534222, 0.06011203,
        0.06211908, 0.68319277, 0.34030356, 0.26849195, 0.05883898,
        0.37259568, 0.85177104, 0.53141152, 0.11695296, 0.73013186,
        0.05868005, 0.19118145, 0.92374115, 0.40840235, 0.6835809 ],
       [0.0803077 , 0.1893075 , 0.70055443, 0.48284417, 0.20130932,
        0.52467516, 0.54734497, 0.36079865, 0.32631029, 0.07459611,
        0.34592097, 0.50444219, 0.37569038, 0.35744269, 0.18466475,
        0.08775113, 0.46061581, 0.90114707, 0.94049296, 0.23930423],
       [0.52935223, 0.49590164, 0.35146561, 0.00771866, 0.29229495,
        0.68584935, 0.1255136 , 0.19971982, 0.73346201, 0.51213047,
        0.46768733, 0.90407055, 0.82762451, 0.12175357, 0.89542905,
        0.07258344, 0.22948567, 0.96548473, 0.22674003, 0.31870152],
       [0.55410522, 0.87324508, 0.79980585, 0.64095256, 0.29683337,
        0.07802534, 0.45419463, 0.36569806, 0.16964579, 0.41355839,
        0.35727195, 0.13078792, 0.08463963, 0.64743208, 0.64189296,
        0.35168302, 0.39599695, 0.19812248, 0.91877611, 0.23550386],
       [0.65416422, 0.21367134, 0.38747314, 0.82068465, 0.12929007,
        0.5461681 , 0.71207599, 0.71685563, 0.77510339, 0.30145219,
        0.71298617, 0.97620726, 0.23433513, 0.13521878, 0.68018848,
        0.09735912, 0.3985609 , 0.95363321, 0.32997877, 0.31743948],
       [0.76982216, 0.0395714 , 0.11986159, 0.13023568, 0.74617636,
        0.78373296, 0.30350781, 0.30281778, 0.91149265, 0.69775049,
        0.56690621, 0.900408  , 0.87716403, 0.96302196, 0.56072209,
        0.95010627, 0.34613038, 0.24320805, 0.77750347, 0.76634203],
       [0.74276879, 0.86975402, 0.63704401, 0.62308415, 0.48459096,
        0.41607366, 0.02747562, 0.52673396, 0.53733691, 0.17104523,
        0.07152666, 0.83350753, 0.19309586, 0.70668821, 0.08741014,
        0.75735304, 0.89656358, 0.00696518, 0.18004327, 0.0669692 ],
       [0.0069334 , 0.44147947, 0.53509785, 0.16366487, 0.65910071,
        0.24187519, 0.447849  , 0.66905146, 0.1523497 , 0.35651314,
        0.32655428, 0.7419353 , 0.69160677, 0.29974135, 0.73811682,
        0.21814573, 0.96069355, 0.23094692, 0.30193065, 0.16580655]])

In [7]: session2 = new_session()                                                

In [8]: session2.run(a)                                                         
Out[8]: 
array([[0.06278953, 0.92966713, 0.15261774, 0.588857  , 0.05788972,
        0.71180712, 0.62486666, 0.47462451, 0.99727786, 0.15202203,
        0.72452653, 0.98535792, 0.0346885 , 0.64527834, 0.83797195,
        0.83395962, 0.39824545, 0.8934134 , 0.541806  , 0.83978902],
       [0.63635538, 0.18294176, 0.69331702, 0.52763233, 0.3229291 ,
        0.62241287, 0.68414848, 0.97999275, 0.55168412, 0.4603575 ,
        0.82990232, 0.90552774, 0.97451053, 0.40686349, 0.54823289,
        0.4247938 , 0.23535826, 0.55458135, 0.55258465, 0.24617388],
       [0.44944288, 0.04010546, 0.55743127, 0.45643243, 0.16292647,
        0.38589025, 0.97295906, 0.2309159 , 0.36286914, 0.6497375 ,
        0.57341974, 0.09344122, 0.01967838, 0.98499235, 0.40975712,
        0.57559919, 0.11186148, 0.74766026, 0.51243854, 0.7151745 ],
       [0.59554684, 0.27307507, 0.37105536, 0.23731401, 0.07928558,
        0.30030247, 0.11912166, 0.30569325, 0.44055106, 0.66682974,
        0.70194368, 0.19419587, 0.33057051, 0.1957851 , 0.91913741,
        0.39932088, 0.39925625, 0.21255173, 0.10834017, 0.40510625],
       [0.58570478, 0.63999024, 0.60711114, 0.35790321, 0.03935359,
        0.29713775, 0.23494106, 0.18004252, 0.65265073, 0.02935006,
        0.56979132, 0.12347523, 0.17526049, 0.61784638, 0.95982497,
        0.64096918, 0.71338032, 0.0979669 , 0.01520631, 0.15262291],
       [0.3135371 , 0.44314403, 0.42788622, 0.96726161, 0.07888723,
        0.60221491, 0.53075983, 0.71256098, 0.5216132 , 0.11446414,
        0.93905272, 0.39946166, 0.00228463, 0.74673268, 0.68813588,
        0.15785701, 0.37980197, 0.07209914, 0.91218897, 0.8224859 ],
       [0.66014014, 0.32113028, 0.90041553, 0.54046677, 0.68700057,
        0.69033259, 0.7842881 , 0.88329396, 0.58093067, 0.81175768,
        0.19481369, 0.01494406, 0.15493862, 0.37604864, 0.348721  ,
        0.34257649, 0.62440358, 0.6258676 , 0.35766471, 0.71645263],
       [0.10860085, 0.18229978, 0.87975443, 0.12793471, 0.0596285 ,
        0.41011133, 0.3194171 , 0.15745823, 0.27606328, 0.19873117,
        0.03263342, 0.13748292, 0.69313661, 0.35918474, 0.4565669 ,
        0.85733087, 0.44626454, 0.10169915, 0.12741476, 0.78491404],
       [0.81891845, 0.00973453, 0.61652424, 0.71913264, 0.34067129,
        0.44458781, 0.52092245, 0.67830463, 0.47626831, 0.56424497,
        0.76968304, 0.73731452, 0.71749442, 0.69947051, 0.46067184,
        0.41743385, 0.56679369, 0.27563535, 0.96041557, 0.20614126],
       [0.92617988, 0.766046  , 0.39547086, 0.48542568, 0.2632229 ,
        0.39583571, 0.71995094, 0.02390021, 0.24162666, 0.69003697,
        0.98268602, 0.62513413, 0.32820124, 0.51544208, 0.17805757,
        0.24546812, 0.15784564, 0.27552066, 0.77633782, 0.88856292]])
```





Discussion:

Resolving patch:
diff --git a/mars/_version.py b/mars/_version.py
--- a/mars/_version.py
+++ b/mars/_version.py
@@ -16,7 +16,7 @@
 import os
 import sys
 
-version_info = (0, 1, 0, 'b1')
+version_info = (0, 2, 0, 'a1')
 _num_index = max(idx if isinstance(v, int) else 0
                  for idx, v in enumerate(version_info))
 __version__ = '.'.join(map(str, version_info[:_num_index + 1])) + \
diff --git a/mars/tensor/expressions/random/core.py b/mars/tensor/expressions/random/core.py
--- a/mars/tensor/expressions/random/core.py
+++ b/mars/tensor/expressions/random/core.py
@@ -33,7 +33,6 @@
 class RandomState(object):
     def __init__(self, seed=None):
         self._random_state = np.random.RandomState(seed=seed)
-        self._curr_seed = seed
 
     def seed(self, seed=None):
         """
@@ -53,11 +52,10 @@ def seed(self, seed=None):
         RandomState
         """
         self._random_state.seed(seed=seed)
-        self._curr_seed = seed
 
     @property
     def _state(self):
-        return State(self._random_state) if self._curr_seed is not None else None
+        return State(self._random_state)
 
     @classmethod
     def _handle_size(cls, size):


Test patch:
diff --git a/mars/tests/test_session.py b/mars/tests/test_session.py
--- a/mars/tests/test_session.py
+++ b/mars/tests/test_session.py
@@ -183,3 +183,14 @@ def testArrayProtocol(self):
 
         result = np.asarray(arr4, dtype=np.float_)
         np.testing.assert_array_equal(result, np.asarray(200, dtype=np.float_))
+
+    def testRandomExecuteInSessions(self):
+        arr = mt.random.rand(20, 20)
+
+        sess1 = new_session()
+        res1 = sess1.run(arr)
+
+        sess2 = new_session()
+        res2 = sess2.run(arr)
+
+        np.testing.assert_array_equal(res1, res2)