Forum archive 2000-2006

Michal Charemza - Different random seed for each login session

Michal Charemza - Different random seed for each login session

by Arnold Pizer -
Number of replies: 0
inactiveTopicDifferent random seed for each login session topic started 9/3/2006; 11:23:50 AM
last post 9/5/2006; 10:38:01 AM
userMichal Charemza - Different random seed for each login session  blueArrow
9/3/2006; 11:23:50 AM (reads: 312, responses: 2)
Hi,

I'm having trouble getting different random numbers for each login session.

I have tried what it says on

http://webwork.math.rochester.edu/docs/docs/pglanguage/pod/pgbasicmacros.html#pseudorandom_number_generator

which is:

SRAND($envir->{'inputs_ref'}->{'key'} )

however, if I log out and then log in again, I get the same random numbers.

How can I get a different set of random numbers for each time I log in?

Michal.

<| Post or View Comments |>


userDavide P. Cervone - Re: Different random seed for each login session  blueArrow
9/4/2006; 8:15:54 AM (reads: 385, responses: 0)
In WW1.x (years ago) I think the key was a number, and so this method of seeding the random generator would make sense. Now, however, the key is a character string, so this won't work (when used as a number, that string will be interpreted as zero, and so no matter what the key is, your seed will be the same).

In order to make this work, you would need to convert the key to a number in some way. There are many possible approaches. Here's one:

  $seed = 0;
foreach $c (split(//,$inputs_ref->{key})) {$seed += ord($c)}
SRAND($seed);
This turns each character into its ASCII character number, and adds up the results. Not particularly sophisticated, but it should get you a different number for most seeds.

Davide

<| Post or View Comments |>


userMichal Charemza - Re: Different random seed for each login session  blueArrow
9/5/2006; 10:38:01 AM (reads: 384, responses: 0)
Thanks - this works as I expect it would now,

Michal.

<| Post or View Comments |>