Random CGI
[1/4] from: al::bri::xtra::co::nz at: 4-Feb-2001 20:57
Terry wrote:
> I have a CGI script that generates a random number...
>
> id: random 1000000
>
> The problem is, it always picks the same number regardless?
Remember to always do:
random/seed now
as Elan pointed out. This restarts the random number generator at
different points depending upon the current time.
Andrew Martin
ICQ: 26227169 http://members.nbci.com/AndrewMartin/
[2/4] from: terry:depotcity at: 3-Feb-2001 21:42
Hello All.
Don't know if this has been asked before?
I have a CGI script that generates a random number...
id: random 1000000
The problem is, it always picks the same number regardless?
957367
Same goes with a script like
n: ["option a " "option b"]
print pick n random (length? n)
works fine in a console, but when used with CGI, always picks "option b"
Go figure?
T Brownell
[3/4] from: rebol:techscribe at: 3-Feb-2001 22:23
Hi Terry,
I'm using the following quick hack in a CGI program that appears to work
well enough:
create-password: func [/local alpha index password] [
alpha:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
random/seed now
password: ""
for i 1 8 1 [
index: random 62
insert password pick alpha index
]
return password
]
Hope this helps,
Elan
Terry Brownell wrote:
[4/4] from: chris:starforge at: 4-Feb-2001 10:18
#04-Feb-01# Message from *Terry Brownell*:
Hi Terry,
> The problem is, it always picks the same number regardless?
Before you use *any* sort of random number generator you need to set the
seed value. random probably has a built-in, fixed value default which is
fine if you need a single run of "somewhat random" values (I doubt that
random uses a decent quality linear congruence or subtractive method
algorithm so don't expect it to be random over long periods) but if you
need to do several "one off" random numbers you should either:
- write a function that takes the current time, plays with it a bit and
returns a value you can use.
- call
random/seed now
before calling random (more or less the same as the first option really)
If you need something "very" random (ie: not related to the time in any
way) then you'll need to use a seed store on the server and load and save
the seed for each session...
Chris
--
New sig in the works
Explorer 2260, Designer and Coder
http://www.starforge.co.uk
--
Cleanliness is next to impossible.