test_random.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import random
  2. from sympy.core.random import random as rand, seed, shuffle, _assumptions_shuffle
  3. from sympy.core.symbol import Symbol, symbols
  4. from sympy.functions.elementary.trigonometric import sin, acos
  5. from sympy.abc import x
  6. def test_random():
  7. random.seed(42)
  8. a = random.random()
  9. random.seed(42)
  10. Symbol('z').is_finite
  11. b = random.random()
  12. assert a == b
  13. got = set()
  14. for i in range(2):
  15. random.seed(28)
  16. m0, m1 = symbols('m_0 m_1', real=True)
  17. _ = acos(-m0/m1)
  18. got.add(random.uniform(0,1))
  19. assert len(got) == 1
  20. random.seed(10)
  21. y = 0
  22. for i in range(4):
  23. y += sin(random.uniform(-10,10) * x)
  24. random.seed(10)
  25. z = 0
  26. for i in range(4):
  27. z += sin(random.uniform(-10,10) * x)
  28. assert y == z
  29. def test_seed():
  30. assert rand() < 1
  31. seed(1)
  32. a = rand()
  33. b = rand()
  34. seed(1)
  35. c = rand()
  36. d = rand()
  37. assert a == c
  38. if not c == d:
  39. assert a != b
  40. else:
  41. assert a == b
  42. abc = 'abc'
  43. first = list(abc)
  44. second = list(abc)
  45. third = list(abc)
  46. seed(123)
  47. shuffle(first)
  48. seed(123)
  49. shuffle(second)
  50. _assumptions_shuffle(third)
  51. assert first == second == third