Page 1 of 1 1
Topic Options
#212643 - 2017-08-25 09:13 AM Random number generation
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
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
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#212644 - 2017-08-25 03:46 PM Re: Random number generation [Re: Mart]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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.


Top
#212645 - 2017-08-25 03:57 PM Re: Random number generation [Re: Allen]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
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

Top
#212646 - 2017-08-25 04:04 PM Re: Random number generation [Re: Allen]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2125
Loc: Tulsa, OK
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.
Top
#212647 - 2017-08-28 04:37 PM Re: Random number generation [Re: ShaneEP]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#212648 - 2017-08-28 04:58 PM Re: Random number generation [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
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.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#212649 - 2017-08-28 05:14 PM Re: Random number generation [Re: Mart]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
Use ABS(@TICKS) as it can be negative - not sure if that's allowed in the seed call.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#212650 - 2017-08-29 10:11 AM Re: Random number generation [Re: Glenn Barnas]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Done. Thanks.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.052 seconds in which 0.022 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org