neilsf / XC-BASIC

A compiling BASIC dialect for the Commodore-64
https://xc-basic.net/
MIT License
74 stars 15 forks source link

rnd!() doesn't always return all expected possible values #140

Closed hiavatch closed 3 years ago

hiavatch commented 3 years ago

Some numbers are not selected, sometimes. One test never selected a 2, with 5000 consecutive calls. Another never produced a 1, or a 45, even with 30000 consecutive calls. Yet another test selected all three of these at least once--all with the same test code:

for biscuit = 1 to 30000
    textat 0,0,"    "
    textat 17,12,"   "
    textat 0,0,biscuit

    n! = rnd!()

    textat 17,12,n! 

    if n!=2 then textat 24,1,"we hit a two!"

    if n!=1 then textat 24,7,"we hit a one!"

    if n!=45 then textat 24,20,"we hit a 45! "
next biscuit

I noticed this when playing with a crude demo to fill the screen really fast with random characters: some character locations remained blank while others changed very often. The above test code was my attempt to figure out what went wrong.

neilsf commented 3 years ago

That's fairly possible as currently nothing guarantees that the random number generator will return all possible values, even if you call RND() many times.

For your reference, XC=BASIC uses this RND routine: https://codebase64.org/doku.php?id=base:16bit_pseudo_random_generator

Seeding the randomizer could still be improved but I'm afraid that it will not solve the above problem.

(Note: the RND() function is going to be entirely rewritten in version 3.)

hiavatch commented 3 years ago

Thanks for the response. I'll code around it until the update. Great project, btw.