Random Number Generator to produce SAME random number from 12:00am11:59pm
Author 
Message 
Phil Powe #1 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
I would like to find some TCL code out there, or someone that would know how to do this successfully.. to produce a random number from 1 to [llength $myRows], where that random number will be the EXACT SAME random number, for everyone hitting your site everywhere on earth, from 12:00am until 11:59pm that day. I have not found an example of this and my code only will pull from a limited range of any row index in $myRows, like Row #1 today, Row #8 tomorrow, Row #2 the next day, but never Row #[expr {[llength $myRows]  1}]. Can someone help me with this one? I'm stumped beyond belief! Thanx Phil

Sun, 23 May 2004 13:46:07 GMT 


David Gravereau #2 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Quote:
>I would like to find some TCL code out there, or someone that would >know how to do this successfully.. to produce a random number from 1 >to [llength $myRows], where that random number will be the EXACT SAME >random number, for everyone hitting your site everywhere on earth, >from 12:00am until 11:59pm that day. >I have not found an example of this and my code only will pull from a >limited range of any row index in $myRows, like Row #1 today, Row #8 >tomorrow, Row #2 the next day, but never Row #[expr {[llength $myRows] > 1}]. >Can someone help me with this one? I'm stumped beyond belief! >Thanx >Phil
Hmm.. if you split the day in half and consider 7 days, you have 14 possible modes. proc getMode {} { foreach {day half} [split [clock format [clock seconds] format "%u %p"]] {} return [expr {(($day  1) * 2) + ($half == "PM" ? 1 : 0)}] Quote: }
Then map that against a table to emulate a randomness... set randTable [list 3 8 2 4 13 11 1 5 9 7 12 0 6 10] set rowToUse [lindex $randTable [getMode]] 
Tomasoft Engineering, Hayward, CA

Sun, 23 May 2004 14:16:39 GMT 


Michael A. Cleverl #3 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Quote: > I would like to find some TCL code out there, or someone that would > know how to do this successfully.. to produce a random number from 1 > to [llength $myRows], where that random number will be the EXACT SAME > random number, for everyone hitting your site everywhere on earth, > from 12:00am until 11:59pm that day.
Here's one (late night) stab at it: package require md5 ;# tcllib 1.0 proc random {max {date ""}} { if {[string length $date] == 0} { set date [clock format [clock seconds] format "%Y%m%d"] } set val 0 foreach hex [split [md5::md5 $date] ""] { scan $hex %x decimal incr val $decimal } return [expr {($val % $max) + 1}] } Michael

Sun, 23 May 2004 15:12:01 GMT 


Sebastian Wangnic #4 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
To get a random number between 0 and $n1: expr {int(rand()*$n)} To get the same random number for every $s: expr {srand($s)} To get the same random number during each day, but a different one every day: expr {srand([clock format [clock seconds] format %j])} These will repeat every year; if you dont want that, use the year for the seed as well: expr {srand([clock format [clock seconds] format %j]1+[clock format [clock seconds] format %Y]*366)}. (hoping that the second [clock seconds] didn't bring us into a new year ...) This uses local time, use gmt 1 if you want that  or do you want to use clock scan on a date provided by the client? If you want to distinguish the morning from the afternoon, try to use [scan [clock format ... format %H] %d hour] and check whether $hour<12, or use Davids trick with [clock format ... format %p] to retrieve the AM/PM indicator (but I believe you meant from midnight to midnight  this AM/PM business is really difficult to grasp for me ...) Regards, Sebastian
Quote: > I would like to find some TCL code out there, or someone that would > know how to do this successfully.. to produce a random number from 1 > to [llength $myRows], where that random number will be the EXACT SAME > random number, for everyone hitting your site everywhere on earth, > from 12:00am until 11:59pm that day. > I have not found an example of this and my code only will pull from a > limited range of any row index in $myRows, like Row #1 today, Row #8 > tomorrow, Row #2 the next day, but never Row #[expr {[llength $myRows] >  1}]. > Can someone help me with this one? I'm stumped beyond belief! > Thanx > Phil

Sun, 23 May 2004 16:12:05 GMT 


Donal K. Fellow #5 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Quote:
> I would like to find some TCL code out there, or someone that would > know how to do this successfully.. to produce a random number from 1 > to [llength $myRows], where that random number will be the EXACT SAME > random number, for everyone hitting your site everywhere on earth, > from 12:00am until 11:59pm that day.
Easy one. # Get the time of midday (in GMT so everyone's in the same boat) set t [clock scan "12:00" gmt 1] # Seed the RNG with that value... expr {srand($t)} # Get that random number! set n [expr { 1+int(rand()*[llength $myRows]) }] Donal. 
 Once more it's a case of Linux having hidden depths compared to Windows'

Mon, 24 May 2004 01:53:23 GMT 


Phil Powe #6 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Looks good until you used rand() then it will fail. I cannot generate an actual random number because this number MUST retain itself for every single user from 12:00am  11:59pm that very same day, so you can't use rand() anywhere, sorry! Phil Quote:
> To get a random number between 0 and $n1: > expr {int(rand()*$n)} > To get the same random number for every $s: > expr {srand($s)} > To get the same random number during each day, but a different one every > day: > expr {srand([clock format [clock seconds] format %j])} > These will repeat every year; if you dont want that, use the year for the > seed as well: > expr {srand([clock format [clock seconds] format %j]1+[clock format > [clock seconds] format %Y]*366)}. > (hoping that the second [clock seconds] didn't bring us into a new year ...) > This uses local time, use gmt 1 if you want that  or do you want to use > clock scan on a date provided by the client? > If you want to distinguish the morning from the afternoon, try to use [scan > [clock format ... format %H] %d hour] > and check whether $hour<12, or use Davids trick with [clock format > ... format %p] to retrieve the AM/PM indicator > (but I believe you meant from midnight to midnight  this AM/PM business is > really difficult to grasp for me ...) > Regards, > Sebastian
> > I would like to find some TCL code out there, or someone that would > > know how to do this successfully.. to produce a random number from 1 > > to [llength $myRows], where that random number will be the EXACT SAME > > random number, for everyone hitting your site everywhere on earth, > > from 12:00am until 11:59pm that day. > > I have not found an example of this and my code only will pull from a > > limited range of any row index in $myRows, like Row #1 today, Row #8 > > tomorrow, Row #2 the next day, but never Row #[expr {[llength $myRows] > >  1}]. > > Can someone help me with this one? I'm stumped beyond belief! > > Thanx > > Phil

Mon, 24 May 2004 02:52:38 GMT 


Phil Powe #7 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
You ALMOST had it down cold, but there is a big kink in yours, I'm afraid. Great, GREAT algorithm though, provided your list is finite. That is, you produce a list of indices from 1 to x. You have to physically rearrange the list each time you add a row to your file, which is not always practical to do especially when working remotely. But I'll definitely keep this in mind, though! Thanx Phil Quote:
> >I would like to find some TCL code out there, or someone that would > >know how to do this successfully.. to produce a random number from 1 > >to [llength $myRows], where that random number will be the EXACT SAME > >random number, for everyone hitting your site everywhere on earth, > >from 12:00am until 11:59pm that day. > >I have not found an example of this and my code only will pull from a > >limited range of any row index in $myRows, like Row #1 today, Row #8 > >tomorrow, Row #2 the next day, but never Row #[expr {[llength $myRows] > > 1}]. > >Can someone help me with this one? I'm stumped beyond belief! > >Thanx > >Phil > Hmm.. if you split the day in half and consider 7 days, you have 14 possible > modes. > proc getMode {} { > foreach {day half} [split [clock format [clock seconds] format "%u %p"]] {} > return [expr {(($day  1) * 2) + ($half == "PM" ? 1 : 0)}] > } > Then map that against a table to emulate a randomness... > set randTable [list 3 8 2 4 13 11 1 5 9 7 12 0 6 10] > set rowToUse [lindex $randTable [getMode]]

Mon, 24 May 2004 02:57:45 GMT 


Phil Powe #8 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Got the weirdest error message on the last one when trying to run this: "Can't use nonnumeric value of $val for operand %" Help! Phil
Quote:
> > I would like to find some TCL code out there, or someone that would > > know how to do this successfully.. to produce a random number from 1 > > to [llength $myRows], where that random number will be the EXACT SAME > > random number, for everyone hitting your site everywhere on earth, > > from 12:00am until 11:59pm that day. > Here's one (late night) stab at it: > package require md5 ;# tcllib 1.0 > proc random {max {date ""}} { > if {[string length $date] == 0} { > set date [clock format [clock seconds] format "%Y%m%d"] > } > set val 0 > foreach hex [split [md5::md5 $date] ""] { > scan $hex %x decimal > incr val $decimal > } > return [expr {($val % $max) + 1}] > } > Michael

Mon, 24 May 2004 03:02:02 GMT 


Bruce Hartwe #9 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Quote:
> Looks good until you used rand() then it will fail. I cannot generate > an actual random number because this number MUST retain itself for > every single user from 12:00am  11:59pm that very same day, so you > can't use rand() anywhere, sorry!
Read the seconds sentence  get the SAME random number using srand by using the same seed (based on day, nt exact time) you will get the SAME random number each time it is run (But I think Donal's post has a more readable version) Bruce Quote:
> > To get a random number between 0 and $n1: > > expr {int(rand()*$n)} > > To get the same random number for every $s: > > expr {srand($s)} > > To get the same random number during each day, but a different one every > > day: > > expr {srand([clock format [clock seconds] format %j])} > > These will repeat every year; if you dont want that, use the year for the > > seed as well: > > expr {srand([clock format [clock seconds] format %j]1+[clock format > > [clock seconds] format %Y]*366)}. > > (hoping that the second [clock seconds] didn't bring us into a new year ...)

Mon, 24 May 2004 05:59:35 GMT 


Phil Powe #10 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Leave it to the Europeans!!! This is BRILLIANT!!!!!!!! No patterns, utilizes the entire llength of myRows and is fully random but remains a constant value from 12:00am  11:59 GMT. Thank you!!! Phil
Quote:
> > I would like to find some TCL code out there, or someone that would > > know how to do this successfully.. to produce a random number from 1 > > to [llength $myRows], where that random number will be the EXACT SAME > > random number, for everyone hitting your site everywhere on earth, > > from 12:00am until 11:59pm that day. > Easy one. > # Get the time of midday (in GMT so everyone's in the same boat) > set t [clock scan "12:00" gmt 1] > # Seed the RNG with that value... > expr {srand($t)} > # Get that random number! > set n [expr { 1+int(rand()*[llength $myRows]) }] > Donal.

Mon, 24 May 2004 10:45:42 GMT 


Sebastian Wangnic #11 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Quote: > # Get the time of midday (in GMT so everyone's in the same boat) > set t [clock scan "12:00" gmt 1]
Nice trick! Quote: > # Seed the RNG with that value... > expr {srand($t)} > # Get that random number! > set n [expr { 1+int(rand()*[llength $myRows]) }]
Note that srand() returns a random number already, so you can optimise the two lines above to: Quote: > # Get a random number, seeded by that value > set n [expr { 1+int(srand($t)*[llength $myRows]) }]

Mon, 24 May 2004 16:22:46 GMT 


Donal K. Fellow #12 / 12

Random Number Generator to produce SAME random number from 12:00am11:59pm
Quote:
> Note that srand() returns a random number already, so you can optimise the > two lines above to: > # Get a random number, seeded by that value > set n [expr { 1+int(srand($t)*[llength $myRows]) }]
Or even: set n [expr {1+int(srand([clock scan 12:00 gmt 1])*[llength $myRows])}] There are even tricks you can play so as to select an item from a list read in from file as you read the file (i.e. without knowing the length of the file.) But unless your list is very long (w.r.t. your free physical memory) *and* you only want to make one selection, it is far easier to slurp the whole lot in and pick using the above, as [llength] and [lindex] are both O(1) operations. :^) Donal.  Itaniumbased servers combined with Microsoft's latest server software offer customers superior performance, greater choice, reliability and investment protection at significantly lower costs than proprietary solutions.  Abhi Talwalkar (Intel VP)

Tue, 25 May 2004 21:41:55 GMT 


