Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a random number wihout RND.
#21
Bill consults with me all the time. I gave him just enough string theory to hang himself.

Pete Big Grin
Fake News + Phony Politicians = Real Problems

Reply
#22
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:
Reply
#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
#24
Oh too early in am to concieve of a "disatrously poor" random number? ;-))

@SierraKen you're back! Smile
b = b + ...
Reply




Users browsing this thread: 1 Guest(s)