Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Create a number based on a date?
Message
 
 
To
11/03/2009 09:40:46
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
General information
Forum:
Visual FoxPro
Category:
Other
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01387096
Message ID:
01387109
Views:
62
>>Hi,
>>
>>I need to create an algorithm that will give me a number based on the current date. This does not have to be very sophisticated. As long as it a notch above the number of days from a certain date, it will do.
>>
>>Thank you in advance for any suggestions.
>
>Getting the difference in days from a certain date certainly seems the most simplest.
>
>I suppose that for some reason you want something more "random-looking"; you can use the difference in days as a seed value for a random number algorithm. That way, it should always produce the same value from the same date. However, being a function of the "hash" type, it is no longer guaranteed that different dates give different numbers; also, a related potential problem (or benefit, depending on your needs) is that you won't be able to calculate the original date from the hash value (the calculated value).
>
>To illustrate what I mean:
>
>
>ldReferenceDate = {^1900-01-01}
>ldSomeDate = {^2009-03-11} && I used today's date
>RAND(ldSomeDate - ldReferenceDate)
>? RAND()
>
>
>Note that because you are always providing the same original "seed value", the "sequence" of random numbers (in this case, a single random number) will always be the same.
>
>Change variable ldSomeDate to some other date - e.g., tomorrow's date - and you will get a different "random number", but once again, reproducible. Meaning that the same date will produce the same value.
>
>As an alternative, you might try to some research on "random sequence" algorithms, and do a similar calculation yourself.
>
>Update: To understand what is going on, note that the "random numbers" are really pseudo-random. Each "random" number is calculated on the basis of the previous number. To get a different sequence each time, you have to start your program with rand(-1). This initializes the random number generator with the system clock. While this is not truly random, the result looks like random numbers (for most purposes - not random enough for certain statistical appications), and you will get different results on each run. In this case, I assume you want to get the same results each time, but in a manner that is somewhat random-looking.

Hilmar,

You algorithm seems to be perfect for my need. Thank you very much for the detailed explanation.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform