ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old August 27th, 2004, 10:15 AM
MatthewClark's Avatar
MatthewClark MatthewClark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: San Angelo, Texas (USA)
Posts: 286 MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 48 m 16 sec
Reputation Power: 7
Send a message via ICQ to MatthewClark Send a message via AIM to MatthewClark Send a message via Yahoo to MatthewClark
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:
function seed_random($how)
{
switch (
$how)
{
case 
'h':
$time time();
$today mktime(00, (int)date("H"$time), (int)date("n"$time),(int)date("j"$time), (int)date("Y"$time));     
srand($today pi());
break;
case 
'd':
$time time();
$today mktime(000, (int)date("n"$time),(int)date("j"$time), (int)date("Y"$time));     
srand($today pi());
break;
case 
'm':
$time time();
$today mktime(0000,(int)date("j"$time), (int)date("Y"$time));     
srand($today pi());
break;
 
default:
srand((double)microtime()*1000000);
}



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...
__________________
InLesserTerms.net
Sometimes it takes a little cussin' to get things done right.

Last edited by MatthewClark : August 27th, 2004 at 10:44 AM.

Reply With Quote
  #2  
Old August 27th, 2004, 10:38 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
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

Reply With Quote
  #3  
Old August 27th, 2004, 10:42 AM
MatthewClark's Avatar
MatthewClark MatthewClark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: San Angelo, Texas (USA)
Posts: 286 MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 48 m 16 sec
Reputation Power: 7
Send a message via ICQ to MatthewClark Send a message via AIM to MatthewClark Send a message via Yahoo to MatthewClark
Good suggestion. Duly noted

Reply With Quote
  #4  
Old August 27th, 2004, 11:12 AM
MatthewClark's Avatar
MatthewClark MatthewClark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: San Angelo, Texas (USA)
Posts: 286 MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 48 m 16 sec
Reputation Power: 7
Send a message via ICQ to MatthewClark Send a message via AIM to MatthewClark Send a message via Yahoo to MatthewClark
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...

Reply With Quote
  #5  
Old August 27th, 2004, 01:13 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
Yeah, I've heard doing most stuff with CF is pretty easy.

Reply With Quote
  #6  
Old August 28th, 2004, 07:26 PM
MatthewClark's Avatar
MatthewClark MatthewClark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: San Angelo, Texas (USA)
Posts: 286 MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 48 m 16 sec
Reputation Power: 7
Send a message via ICQ to MatthewClark Send a message via AIM to MatthewClark Send a message via Yahoo to MatthewClark
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.

Reply With Quote
  #7  
Old August 28th, 2004, 07:31 PM
MatthewClark's Avatar
MatthewClark MatthewClark is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: San Angelo, Texas (USA)
Posts: 286 MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level)MatthewClark User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 13 h 48 m 16 sec
Reputation Power: 7
Send a message via ICQ to MatthewClark Send a message via AIM to MatthewClark Send a message via Yahoo to MatthewClark
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Random Number based on Day


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway