Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a random number wihout RND.
#23
(08-10-2024, 05:15 AM)SierraKen Wrote: Hi guys, been a few years and I thought I would play my hand in some programming again. So I saw this thread and tried it out.
I will admit I used Chat GPT for some of it, but someone has to learn someway. I changed it a bit also making the user able to choose
the highest number and also I added more randomness to it by using the seconds in time$ and then dividing that by 10 and then using that variable
to times it by timeSeed. It works pretty good. Probably not as random as RND, but what do you think?

Code: (Select All)

Cls
start:
s$ = Time$
s1$ = Right$(s$, 2)
s2 = Val(s1$)
s = s2 / 10
Print "Type the highest number for me to choose from."
Input "->", most
If most < 1 Or most > 1000000000 Then GoTo start:
timeSeed = Timer ' Get the system time in seconds
timeSeed = timeSeed * s
randomNumber = (timeSeed Mod most) + 1
Print "Random Number: "; randomNumber
Print: Print: Print
GoTo start:


This is very close to a Linear Congruential Generator (LCG) which is basically the formula V = (V*A+C) Mod M.
(see: https://en.wikipedia.org/wiki/Linear_con..._generator).


On the good side:
  • LCGs are small, simple, and fast.  This is why LCGs have been used in microcomputer BASICs since the 8-bit, 4K RAM days, and are still used by QB64.
  • Once you understand LCGs, one can be easily thrown together when needed if you don't have a built-in generator available.

BUT:
  • The quality of an LCG depends heavily on the parameters (the multiplier A, and the modulus value M) that are used.  Poorly chosen parameters can give disastrously poor results.  (See RANDU: https://en.wikipedia.org/wiki/RANDU)


Often the timer or system clock is used as a startup seed value for the accumulator V ("Randomize Timer"), while the multiplier and modulus were chosen (carefully we hope!) when the function was written and never change.
I'm concerned that using the timer to set the multiplier as well could result in random output quality which is sometimes acceptable, sometimes not.
Reply


Messages In This Thread
Getting a random number wihout RND. - by Dav - 09-30-2023, 11:01 PM
RE: Getting a random number wihout RND. - by Dav - 09-30-2023, 11:06 PM
RE: Getting a random number wihout RND. - by Dav - 09-30-2023, 11:27 PM
RE: Getting a random number wihout RND. - by Dav - 10-01-2023, 01:45 AM
RE: Getting a random number wihout RND. - by Pete - 07-17-2024, 07:00 PM
RE: Getting a random number wihout RND. - by Pete - 07-17-2024, 07:42 PM
RE: Getting a random number wihout RND. - by Pete - 07-17-2024, 09:00 PM
RE: Getting a random number wihout RND. - by Pete - 07-17-2024, 10:08 PM
RE: Getting a random number wihout RND. - by JRace - 08-10-2024, 09:01 AM



Users browsing this thread: 5 Guest(s)