Sign in to follow this  
Followers 0
frew

Does SRandom() have to affect all Random() that follow it?

5 posts in this topic

#1 ·  Posted (edited)

Hello,

Once SRandom() is started for a certain Random(), how can I turn it off for other Random() later in my script?

I do not want the SRandom() to affect all the Random() in my script, just certain Random() along the way.

I use Random() throughout my script, and I want some seeded and others not seeded.

I do not want to have to declare all the Random() [edit: ie all the Random() that I do not want to be affected by the SRandom()] variables at the top of the script before SRandom() starts.

For example, in this code, I want $x to always be affected by the SRandom (which it is), but I want $i to always be a different random number, ie not affected by the SRandom.

SRandom(125)
$x = Random(1, 100, 1)
MsgBox(0, '', $x, 1);$x is always 23 here, which is what
                 ; I want, a seeded random number, very good

$i = Random(1, 100, 1)
MsgBox(0, '', $i, 1);$i is always 55 here, but I want it to be always
                 ;a different random number, not 55 each time, so
                 ;how do I cancel SRandom() for this one?

So, within a given script, is it possible to "cancel" SRandom() along the way where I want a non-seeded Random()?

Or is it the case that once I use an SRandom() in a script, every following Random() will have to be affected by the SRandom() that has been used?

Thanks for any ideas about this.

frew

Edited by frew

Share this post


Link to post
Share on other sites



Random generators always start from a seed.

Share this post


Link to post
Share on other sites

Or is it the case that once I use an SRandom() in a script, every following Random() will have to be affected by the SRandom() that has been used?

True.

If what you wanted was one string of "random numbers" that are the same every time, you could peel them off first with a fixed SRandom seed,

Then run SRandom() with some sort of seed that will change each time, as an example:

SRandom(@sec+TimerInit())

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Oh, nice...that's a very useful idea TurionAltec. Thanks so much for this

SRandom(@sec+TimerInit())
.

That works very nicely.

I was also thinking something like this, putting a random variable into any SRandom() that I that I wish to have sort of "Cancel" any previous SRandom:

$sr_Not_1 = Random(1, 12341234, 1)
$sr_Not_2 = Random(1, 12341234, 1)

SRandom(12345)
$x = Random(1, 100, 1)
MsgBox(0, '', $x, 1)

SRandom($sr_Not_1)
$i = Random(1, 100, 1)
MsgBox(0, '', $i, 1)

SRandom($sr_Not_2)
$z = Random(1, 100, 1)
MsgBox(0, '', $z, 1)

...but your idea is easier.

Thanks again!

frew

Edited by frew

Share this post


Link to post
Share on other sites

It's usually best to set the random seed to a time based value. Then as long as you don't travel back in time, you never use the same value again.

Just call something like SRandom(TimerInit()).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0