Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Numbers
#1
I was curious to know how many random numbers were generated 
by the Randomize function, so I wrote this short prog:

Code: (Select All)
'set start point
DefLng C: DefDbl F, L, D
startat = Int(Rnd * 1000) ' set random Start point in the "list"
For a = 1 To startat: n = Rnd: Next '  generate each random number in turn

First = Rnd: Print First '  set this random number as start point
While First <> last '  wait for the next time this number is found
    count = count + 1  
    last = Rnd ' read each random number
    Print count; last; " "; '  show random number and how many random numbere were read
Wend

{I found it interesting, anyway!)
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#2
https://qb64phoenix.com/forum/showthread.php?tid=3644 <-- Our formulas for RND.

The answer you're looking for here is in the MOD.  It's 2 ^ 24, or 16,777,216.  That's the max number of random numbers we generate before repeating the cycle over again.
Reply
#3
(08-25-2025, 12:12 AM)SMcNeill Wrote: https://qb64phoenix.com/forum/showthread.php?tid=3644 <-- Our formulas for RND.

The answer you're looking for here is in the MOD.  It's 2 ^ 24, or 16,777,216.  That's the max number of random numbers we generate before repeating the cycle over again.

That's the same result as is produced by my prog (plus 1).
I hadn't seen that Mod, so thanks. I got a new perspective
though, and I achieved what I wanted.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#4
QB64 uses a Linear Congruential Generator (LCG), much like the generators used by older Basic interpreters & compilers.

The quality of an LCG's output is totally dependent on the parameters used by the generator.

Below is a pseudo-3D plot of QB64PE's RND output:

   

Notice how the purple points pretty much fill the volume of the displayed cube.  As LCGs go, the one used by QB64PE seems to do a pretty decent job, although I don't know if it generates EVERY possible random number within its range.




BUT!
If an LCG's parameters are chosen poorly then the output WILL NOT generate every possible number, as shown in this plot of numbers generated by the notorious RANDU LGC:

   

RANDU's output falls on one of 15 planes.  All of that whitespace in the image represents numbers that will NEVER be generated no matter how many times you call RANDU.


TLDR, QB64PE's RND function seems to be pretty respectable.


(Sorry I didn't use PE to create these images.  Once upon a time I started a PE program to display side-by-side the outputs of different PseudoRandom Number Generators, but feature creep and limited time killed that project.)
Reply
#5
(08-25-2025, 07:05 PM)JRace Wrote: QB64 uses a Linear Congruential Generator (LCG), much like the generators used by older Basic interpreters & compilers.

The quality of an LCG's output is totally dependent on the parameters used by the generator.

Below is a pseudo-3D plot of QB64PE's RND output:



Notice how the purple points pretty much fill the volume of the displayed cube.  As LCGs go, the one used by QB64PE seems to do a pretty decent job, although I don't know if it generates EVERY possible random number within its range.




BUT!
If an LCG's parameters are chosen poorly then the output WILL NOT generate every possible number, as shown in this plot of numbers generated by the notorious RANDU LGC:



RANDU's output falls on one of 15 planes.  All of that whitespace in the image represents numbers that will NEVER be generated no matter how many times you call RANDU.


TLDR, QB64PE's RND function seems to be pretty respectable.


(Sorry I didn't use PE to create these images.  Once upon a time I started a PE program to display side-by-side the outputs of different PseudoRandom Number Generators, but feature creep and limited time killed that project.)

Thanks, JRace. I believe my little prog (top of this thread) finds all possible results  (16777215) for our RND function. Yes, I guess that's respectable and suits most of our applications.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#6
I had a new perspective once, but my old perspective got better mileage.

Pete Confused
Reply
#7
(08-27-2025, 09:43 PM)Pete Wrote: I had a new perspective once, but my old perspective got better mileage.

Pete Confused

Sounds like my damn new electric tractor.  Thing doesn't get ANY mileage no matter how much gas I pour on it.  It just sits there and burns!
Reply
#8
Big Grin Big Grin Big Grin 

I really wonder how much good we are getting out of this go green garbage. I mean I paid good money for my solar powered night vision goggles, but I'm still getting my ass kicked at midnight paintball! I knew I should have bought the gasoline powered ones!

Pete
Reply
#9
That pseudo-3D chart is a neat way to visualize the randomness. Well done!
Reply
#10
Hi,
A few years ago, I needed to obtain a long series of truly randomized numbers; I used the tools offered by https://random.org. If you are not familiar with it, I encourage you to rush to their site.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Strings - Numbers Kernelpanic 0 110 01-20-2026, 10:29 PM
Last Post: Kernelpanic
  using pseudo-random numbers for repeatable content - kind of a neat trick madscijr 5 1,003 02-13-2025, 11:12 PM
Last Post: Pete
  random numbers badger 8 1,383 11-08-2024, 02:22 AM
Last Post: DANILIN
  Identifying colour attributes with negative numbers CharlieJV 1 575 01-28-2023, 05:16 AM
Last Post: CharlieJV
  Anybody use decimals for line numbers? Pete 6 1,108 08-05-2022, 03:28 AM
Last Post: bplus

Forum Jump:


Users browsing this thread: 1 Guest(s)