Example

Random Number Generator

2

Cheems 2022-10-15 20:33 (Edited)

First Program
Nothing fancy just random numbers up to a certain point it returns decimal values, don’t know why.

Update:
Added RANDOMIZER TIMER at the start of my code. Thanks to Was8bit for explaining why, also shows the seed that was used to generate the number.

Although the Randomizer Timer only refreshes every user restart it should be enough so it won’t be blatantly predictable like in the first version.


was8bit 2022-10-16 04:19 (Edited)

Do you mean something like 7.64142E+08

This is a format for showing big numbers... lowres cannot handle all the digits for really big numbers... like 7,641,428,406


Cheems 2022-10-16 04:55

Oh that makes sense, thank you


was8bit 2022-10-16 10:40

Glad to be helpful :)

... also, i recommend adding RANDOMIZE TIMER at the very top of the code...

... no computer has real random generation.. it is simulated using a built in set of math equations... but an initial # is needed to get it started... without the recommanded command above, zero is generally always used, which means you will always get a predictable series of numbers..

Try this experiment with your current program... start it, then add 12345, you will get 9434 ... now enter 56789 , you will get 20310

... if you close the program, and run it again, and enter these 2 inputs again, you will get the same results again..

Although the RND function is giving different results as you keep using it, over time if you are entering the same inputs, you will be able to remember the results...

... so to help create a better sense of randomness, the command RANDOMIZE TIMER to "start" the random formulas with a new starter # each time the code is run...


was8bit 2022-10-16 10:43 (Edited)

Try this code...

DO
PRINT TIMER
LOOP

TIMER is LowresNX way of keeping precision timing... its the "heartbeat" ... you can use it to track the passage of time with great precision...

As you can see, this number is constantly and rapidly changing...


was8bit 2022-10-16 10:56

Now, RANDOMIZE # command can accept any number, so it is possible if you WANT a predictable set of "random" numbers, you can use RANDOMIZE # to create a "world" that always looks the same with the same input #...

Try this code...

RANDOMIZE 2
CLS
FOR X=0 TO 19
FOR Y=0 TO 15
R=RND(6)
S$=" "
IF R=5 THEN S$="^"
IF R=6 THEN S$="-"
TEXT X,Y,S$
NEXT Y
NEXT X

Entering a different #, say from 1 to 100, will yield 100 landscapes.. each different from each other, BUT every time you run #2 as the "seed" or initializer #, the results will always be the same... ;)


Cheems 2022-10-16 16:16

Wow thank you so much for your advice, I will implement this.


was8bit 2022-10-17 03:12

:)

If you WANT a random decimal #, for any reason, just use R=RND and R will be a decimal number between 0 and 1

I Iike RND(#) the best, but i thought i would share all available options :)


was8bit 2022-10-18 03:09 (Edited)

Nice colors :)

And yes, each use of RND(#) will use the next step in the internal random formulas... once you have "initialized" it with its seed, you are good to go ;)

my guess there are 10,000,000 possible different seeds when you are using RANDOMIZE TIMER ;)


Log in to reply.