File: plot_random_generator.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (50 lines) | stat: -rw-r--r-- 1,190 bytes parent folder | download
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
"""
Random generator parametrization
================================
"""

# %%
# The seed of the pseudo random generator is initialized to 0 when the module is imported.
#
# It means the same script will yield the same results across several replications.
#
# It is also possible to initialize the random generator differently:
#
# - by setting a seed
# - by setting the complete generator state

# %%
import openturns as ot
import os
import time


# %%
# Example 0: using the time in milliseconds
# -----------------------------------------
ot.RandomGenerator.SetSeed(int(1000 * time.time() % 1e9))

# %%
# Example 1: using the python process id
# --------------------------------------
ot.RandomGenerator.SetSeed(os.getpid())

# %%
# Example 2: using a fixed seed
# -----------------------------
ot.RandomGenerator.SetSeed(77)

# %%
# Example 3: using a previously saved generator state
# ---------------------------------------------------

# use the random generator
ot.Normal().getSample(100)
# save the generator state
state = ot.RandomGenerator.GetState()
ot.Normal().getRealization()

# %%
# load the generator state
ot.RandomGenerator.SetState(state)
ot.Normal().getRealization()