Mart
(KiX Supporter)
2017-08-25 09:13 AM
Random number generation

Can someone explain to me why the first example works fine but the second and third always give me the number 0? I tried with Kix 4.60 up until 4.67. All gave the same results.

What I would like to do is get a random(ish) number between 1 and 10 (1 and 10 included) so I thought using the @WDAYNO macro would be nice to use when seeding the random number generator. That did not work so I gave it a number and that also did not work. The only way to get it too work was by feeding it @TICKS. I guess somehow I am missing something.

I have to admit that this is the first time I’m using srnd() and rnd(). I never needed it before.

 Code:
Break on

?@KIX

SRnd(@TICKS)
$randomnumber = Rnd(10)
?$randomnumber

Sleep 5

 Code:
Break on

?@KIX
SRnd(@WDAYNO)
$randomnumber = Rnd(10)
? $randomnumber

Sleep 5

 Code:
Break on

?@KIX
SRnd(5)
$randomnumber = Rnd(10)
? $randomnumber

Sleep 5


AllenAdministrator
(KiX Supporter)
2017-08-25 03:46 PM
Re: Random number generation

Its been a while since this topic has come up, but I seem to remember you needing to use rnd twice. The first time is a result you throw away, and the second is actually the random number. Also remember using @ticks. Aside from that I can't explain the logic. \:\)

edit: and I could be completely off here too. It really has been a while since this came up.



AllenAdministrator
(KiX Supporter)
2017-08-25 03:57 PM
Re: Random number generation

I had a feeling it was Richard H. giving the advice \:\) check out the thread below.
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=166125#Post166125


ShaneEP
(MM club member)
2017-08-25 04:04 PM
Re: Random number generation

It seems that Rdn() is simply not very random. If you use the same seed, it will cycle thru the same 'random' numbers everytime. Which is why when you use @Ticks, or @MSecs, it seems more random. Since the starting point is different each time.

Glenn BarnasAdministrator
(KiX Supporter)
2017-08-28 04:37 PM
Re: Random number generation

Couple of "rules" that I follow:
1. Always seed the RNG with a multi-digit value (@MSECS or Abs(@TICKS)) ONCE and NEVER MORE THAN ONCE!
2. Immediately call Rnd() and discard the value.
3. To improve randomness, especially for small values, take a large value and use Int(SubStr(s,e,l)) or use two randoms and divide one by the other. For ranges that include zero, use a larger range (1-100) and subtract 1.
4. Remember that for a given seed, the random sequence is always the same. This can be used to your advantage - I use it to consistently generate "random" code-wheels for my Enigma-based Cipher function. (WWII German Enigma style encode/decode with 1024 codewheels instead of 6.)

The first response isn't not-random, it's just somewhat less random than following responses, with zero and other single-digit values coming up more frequently than following calls. Thus, it's often better to discard it.

Glenn


Mart
(KiX Supporter)
2017-08-28 04:58 PM
Re: Random number generation

Ok, seems like getting a piece of software to generate a random number is not as easy as one would think. I guess humans work the same way; just count how many people say seven when asked to give a number between 1 and 10. Somehow, seven comes up a lot. How good are people at picking random numbers?

I have it up and running now seeding it once with @ticks, discarding the first random-ish number and using the second.


Glenn BarnasAdministrator
(KiX Supporter)
2017-08-28 05:14 PM
Re: Random number generation

Use ABS(@TICKS) as it can be negative - not sure if that's allowed in the seed call.

Mart
(KiX Supporter)
2017-08-29 10:11 AM
Re: Random number generation

Done. Thanks.