|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
Random Number based on Day
I am a PHP veteran who is new to ColdFusion. So far, I have rewritten an entire PHP website in ColdFusion, and it has gone really well for me.
One thing I can't figure out is this: In PHP, I wrote a function that seeds the random number generator which will make the random number generator return the same random number every time during the same day, hour, minute, in which the generator was seeded, respectively. PHP Code:
I am trying to rewrite this function in ColdFusion, but I'm failing (I've managed to rewrite all my other functions fine). CFLib.org has nothing like this. I'm not trying to get anyone to do my work for me - I'm just not familiar enough with ColdFusion to know how to rewrite this function. Actually, I don't have to rewrite this one. I just need a function in ColdFusion that will seed random number generator to generate the same random number for an entire day... Last edited by MatthewClark : August 27th, 2004 at 10:44 AM. |
|
#2
|
|||
|
|||
|
I'll take a peek at the function when I have more time. But what about saving the random number along with a time stamp as an application-scoped variable? This makes the random number persistent and it will stay in memory until the server is restarted, until the application variable times out, or until you manually refresh it. Then you do this once per day, and just use the variable you've got in the application scope instead of re-running the function on every page request.
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
||||
|
||||
|
Good suggestion. Duly noted
![]() |
|
#4
|
||||
|
||||
|
Okay, I got it:
<cfscript> number = '.' & DateFormat(Now(), "yyyymmdd"); Randomize(number); </cfscript> I just seeded the random number generator with today's date, prepending a period. This returns the same random number all day. Geeze, makes me wonder why I wrote the PHP function before -- this was sooooo easy... |
|
#5
|
|||
|
|||
|
Yeah, I've heard doing most stuff with CF is pretty easy.
![]() |
|
#6
|
||||
|
||||
|
Actually, what I wrote does NOT work. Somehow, it seeds the random number generator to ALWAYS return the same number no matter what I set the seed to.
What I am doing is seeding the random number generator using the numeric representation of today's date. Then, I cound the number of quotes in my daily quotes table, and then perform a RandRange between 1 and the number of records to pull out the specific quote for the day. For some reason, it pulls out the same quote every day, no matter what the seed it set to. |
|
#7
|
||||
|
||||
|
I think I figured it out: I thought I had to seed the Randomizer with a number between 0 and 1. I'd get a numeric representation of today's date using the mask "yyyymmdd", and then prepend a period in front to make it less than 1.
Well, I removed the period, and it seems okay now. If anyone who know better how to do this, please speak up! I am probably stupid. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Random Number based on Day |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|