This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-get-set-state.py
117 lines (102 loc) · 2.33 KB
/
random-get-set-state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import random
state = random.getstate()
# print(state)
print("Get State")
print(random.random())
print(random.random())
print(random.random())
print("Set State")
random.setstate(state)
print(random.random())
print(random.random())
print(random.random())
print("Set State Again")
random.setstate(state)
print(random.random())
print(random.random())
print(random.random())
# برای اینکه بتوانیم دنباله ای از اعداد رندم را دوباره
# تکرار کنیم از استیت استفاده میکنیم - دقت کنید بین هر
# دو استیت برنامه اعداد رندم دوباره تکرار شده اند
"""
یکبار اجرای برنامه
Get State
0.8424125864258368
0.5868829522333908
0.1852885894912859
Set State
0.8424125864258368
0.5868829522333908
0.1852885894912859
Set State Again
0.8424125864258368
0.5868829522333908
0.1852885894912859
"""
"""
اجرای دوم برنامه
Get State
0.7787255862057021
0.7453083030875739
0.0054086510034494495
Set State
0.7787255862057021
0.7453083030875739
0.0054086510034494495
Set State Again
0.7787255862057021
0.7453083030875739
0.0054086510034494495
"""
# اما تفاوت استیت با سیید در چیست؟
# اگر برنامه را دوباره اجرا کنید اعداد رندم در اجراهای
# بعدی تغییر میکنند
# اما
# از سیید به این دلیل استفاده میکنیم که در اجراهای متوالی
# بتوانیم به اعداد رندم یکسان دسترسی داشته باشیم
print("#"*100)
print("Seed = 30")
random.seed(30)
print(random.random())
print(random.random())
print(random.random())
print("Seed = 30")
random.seed(30)
print(random.random())
print(random.random())
print(random.random())
print("Seed = 30")
random.seed(30)
print(random.random())
print(random.random())
print(random.random())
"""
اجرای اول برنامه
Seed = 30
0.5390815646058106
0.2891964436397205
0.03003690855112706
Seed = 30
0.5390815646058106
0.2891964436397205
0.03003690855112706
Seed = 30
0.5390815646058106
0.2891964436397205
0.03003690855112706
"""
"""
اجرای دوم برنامه
Seed = 30
0.5390815646058106
0.2891964436397205
0.03003690855112706
Seed = 30
0.5390815646058106
0.2891964436397205
0.03003690855112706
Seed = 30
0.5390815646058106
0.2891964436397205
0.03003690855112706
"""