I am trying to make a random number by standard use of srand and time. How to I use the clock in the calc to do this?
| Author | Comment | ||
|---|---|---|---|
Drustanos |
Use of time for random numbers |
Lead | |
|
I am trying to make a random number by standard use of srand and time. How to I use the clock in the calc to do this?
|
|||
Lionel Debroux |
|||
|
A seed that should be reasonably random is FiftyMsecTick. You may want to couple it with a Linear Feedback Shift Register.
One more note: the linear congruential random number generator proposed by TIGCCLIB is simple and fast, but the sequence of generated numbers is of mild randomness. Some values are significantly more likely to get produced than the majority of others. Run the TICT "snow" example to see what I mean. |
|||
Drustanos |
|||
|
Thanks, I will try this.
|
|||
Drustanos |
|||
|
Please forgive my ignorance, I am just beginning to learn C.
I am unsure how to use FiftyMsecTick. Obviously "srand( FiftyMsecTick )" does not work. |
|||
Kevin Kofler |
|||
|
FiftyMSecTick needs a minimum AMS version of at least 2.00.
A portable method is:
// compute random seed from 0x600017 and FiftyMSecTick
// NOTE: PedroM 0.80 alpha 3 or higher actually supports this, but still
// claims to be AMS 1 and not to support any AMS 2 functions. And it also
// has the same kernel version number as older 0.80 builds which don't
// support it. (OK, they are alphas and I could just forget about them, but
// still.)
// So here's how we detect it. We first check if it is PedroM. Then we check
// the kernel version number. It must be >=0x0080. And then we sort out the
// old 0.80 alpha build by checking if there is still a ROM_CALL at 0x3cd
// or the "1.48" string.
unsigned long randnum=(unsigned char)-peekIO(0x600017);
if (!AMS_1xx
|| (*(short*)0x32==('R'<<8)+'O' && *(unsigned short*)0x30>=0x80
&& 0x3cd[*(long**)0xc8]!=((long)'1'<<24)+((long)'.'<<16)
+((long)'4'<<8)+(long)'8'))
randnum+=(*((volatile unsigned long*)(_rom_call_addr(4FC))))*
((HW_VERSION>=2)?53:79);
srand(randnum);
|
|||