Ask Your Question
2

Easy way to get repeatable random number sequences.

asked 2011-01-12 08:40:26 +0100

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2011-01-12 08:46:11 +0100

DSM gravatar image

updated 2011-01-12 08:47:30 +0100

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!

edit flag offensive delete link more

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 ( 2011-01-12 09:43:54 +0100 )edit

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 ( 2011-01-12 09:50:32 +0100 )edit

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: 2011-01-12 08:40:26 +0100

Seen: 1,027 times

Last updated: Jan 12 '11