First time here? Check out the FAQ!

Ask Your Question
2

Easy way to get repeatable random number sequences.

asked 14 years ago

v_2e gravatar image

Hello!
Tell me please, if there is a simple and easy way to get the same random number sequence multiple times in Sage?
For example, in Python one can use the random.seed() function to set the random seed and then get a random nuber sequence with random.random() function. After that it is easy and obvious to set the random.seed() to the same seed as the first time and get the very same random sequence again.
But how to get such sequences in Sage?

Thanks.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 14 years ago

DSM gravatar image

updated 14 years ago

sage: set_random_seed(10)
sage: initial_seed()
10L
sage: random()
0.96172216248923448
sage: random()
0.4035526020095398
sage: set_random_seed(10)
sage: initial_seed()
10L
sage: random()
0.96172216248923448
sage: random()
0.4035526020095398

And please read the very useful tutorial in help(sage.misc.randstate) for everything you could ever want to know!

Preview: (hide)
link

Comments

Thank you! In fact, I read that page in tutorial, but didn't quite understand it for some reason. Probably, because I'm slow-witted. :)

v_2e gravatar imagev_2e ( 14 years ago )

No worries! And just to be clear, the "initial_seed()" call was just to check that it was indeed set to 10 (because that's what initial_seed returns). It just spits out what the seed is, it doesn't initialize anything, so you don't need to use it in real code. Unless you've set the seed randomly and you want to know what it was set to so you could use the same seed later, etc.

DSM gravatar imageDSM ( 14 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 14 years ago

Seen: 1,038 times

Last updated: Jan 12 '11