Author Topic: Question about RND e RANDOMIZE TIMER  (Read 3134 times)

0 Members and 1 Guest are viewing this topic.

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Question about RND e RANDOMIZE TIMER
« on: January 27, 2021, 04:47:19 pm »
I have not understood at all the command RND.
As an exemple:
flipB% = INT(RND * 4) + 1
PRINT flipB%

generates and prints a ramdom number between 1 and 4, but it is always 3. So: why 3, and, if it is a random number, why always only 3?

If I want a true random numer, I have to tape:
RANDOMIZE TIMER
flipB% = INT(RND * 4) + 1
PRINT flipB%

in this case, every time I run the program, I can actually have a number between 1 and 4.

so, if RND alone don't generate a random numer, what is its utility without RANDOMIZE TIMER? why RND don't generate itself a random number? And what is the use of RANDOMIZE TIMER without RND?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Question about RND e RANDOMIZE TIMER
« Reply #1 on: January 27, 2021, 05:32:57 pm »
Without RANDOMIZE the RND sequence is always the same, handy for debugging programs using RND.

RANDOMIZE seed
just gives you a different random number sequence, depending on what seed you provide.

The sequences are always the same according to seed. TIMER is nice for seed because you get a different seed every second (or less?). Update: Help says, "every run".

Update: Oh forgot about USING
Code: QB64: [Select]
  1. seed1 = 10: seed2 = 133
  2.  
  3.     RANDOMIZE USING seed1
  4.     FOR i = 1 TO 20
  5.         flipB% = INT(RND * 4) + 1
  6.         PRINT flipB%;
  7.     NEXT
  8.     PRINT: PRINT
  9.     RANDOMIZE USING seed2
  10.     FOR i = 1 TO 20
  11.         flipB% = INT(RND * 4) + 1
  12.         PRINT flipB%;
  13.     NEXT
  14.     PRINT: PRINT
  15.     PRINT "Press any... to repeat, esc to quit "
  16.     SLEEP
  17.  
« Last Edit: January 27, 2021, 05:53:54 pm by bplus »

Offline bartok

  • Newbie
  • Posts: 80
    • View Profile
Re: Question about RND e RANDOMIZE TIMER
« Reply #2 on: January 28, 2021, 02:52:32 am »
I sort of get it. But, if RND without RANDOMIZE doesn't generate a random number at all; if we have always the same sequence with the same RANDOMIZE seed, if, without a RANDOMIZE seed, we have also the same sequence eveytime and everywhere, for example the firs 20 number with RND (1 to 4) without a seed are:
3 3 3 2 2 4 1 4 4 3 1 2 4 4 2 4 4 1 4 2

then, why not simply
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4?  Why just 3 3 3 2 2 4 1 4 4 3 1 2 4 4 2 4 4 1 4 2? In other words, if in order to have a really random sequence we have to put a seed or use RANDOMIZE TIMER, why RND without randomize uses just this sequence that only seems random?

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Question about RND e RANDOMIZE TIMER
« Reply #3 on: January 28, 2021, 03:05:01 am »
Unfortunately the best answer I can give you is "QBasic compatibility".

I agree that, in general, it would be better if the RND sequence were truly random.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Question about RND e RANDOMIZE TIMER
« Reply #4 on: January 29, 2021, 02:25:42 pm »
If the problem is having a "true" random every time you start code then use Randomize Timer.

I think RND always being same sequence helpful when developing code, just add Randomize Timer at start of code when it's all working the way you want. Only on very rare occasions you need to call Randomize again. The one instance I can think of is using Randomize Using Seed to repeat sequences which comes in handy from having to save tons of data to regenerate random drawn screens and then needing "true" random numbers again ie fireworks displays or explosions.
« Last Edit: January 29, 2021, 02:27:40 pm by bplus »

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Question about RND e RANDOMIZE TIMER
« Reply #5 on: January 29, 2021, 09:38:48 pm »
I agree that, in general, it would be better if the RND sequence were truly random.

Depends, no? Many times, you must have a repeatable set of values. For example, in crypto algorithms. A man in the middle will see gibberish which is hard to decrypt, but the legitimate guy at the receiving end has to re-create the exact sequence the sender used. Or, in simulation programs, to debug your program, you may also need to test with the same pseudo-random sequence. So, to me, RANDOMIZE USING, which starts the sequence at the same place for any given seed value, is very useful indeed!
« Last Edit: January 29, 2021, 09:41:41 pm by Bert22306 »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Question about RND e RANDOMIZE TIMER
« Reply #6 on: January 29, 2021, 09:49:41 pm »
+ 1 @Bert22306

Pete

I took a chance once, but it was predictable.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Question about RND e RANDOMIZE TIMER
« Reply #7 on: January 29, 2021, 10:28:49 pm »
People talk about how it’s not random, but I‘ve never yet seen anyone who can successfully predict what number is going to be generated.

PRINT INT(RND(-TIMER) * 100)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Question about RND e RANDOMIZE TIMER
« Reply #8 on: January 29, 2021, 11:41:12 pm »
People talk about how it’s not random, but I‘ve never yet seen anyone who can successfully predict what number is going to be generated.

PRINT INT(RND(-TIMER) * 100)

I predict it's not going to be anything that ends in .5  ;-))