Author Topic: RND and RANDOMIZE information  (Read 17579 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: RND and RANDOMIZE information
« Reply #15 on: June 12, 2019, 11:21:44 am »
Fireworks, landscapes, water reflections, and reusing random number sequences?

Sign me up!  I'll check it out once I'm back on Desktop, sounds very cool.

Are you reusing the random sequence to recreate the same firework as a reflection in the water?

Could you not just draw the firework twice at the time of generating the random numbers?

Not for reflections, that was POINT work.

I used Randomize seed to avoid having to array all the data of for tracking "tails" of a firework burst. Type Structures don't do arrays (yet) so I used a seed for Randomize.

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: RND and RANDOMIZE information
« Reply #16 on: June 12, 2019, 11:31:09 am »
I used Randomize seed to avoid having to array all the data of for tracking "tails" of a firework burst. Type Structures don't do arrays (yet) so I used a seed for Randomize.

Ah, nice.

I had to work around not being able to store an array of variable-length strings in a custom data type recently.  In my case, I created an array of strings separately, then in the data type I had an integer referencing the element in the string array.  Not as clean code, but gets the job done!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: RND and RANDOMIZE information
« Reply #17 on: June 12, 2019, 11:36:30 am »
Ah, nice.

I had to work around not being able to store an array of variable-length strings in a custom data type recently.  In my case, I created an array of strings separately, then in the data type I had an integer referencing the element in the string array.  Not as clean code, but gets the job done!

Ha! I am using variable length strings in custom type now to save small integer arrays (hands by card index) in Blackjack thread. We can do variable length strings in Type definitions now v1.3 :)

Yeah but arrays of string... ? could be done too with a split / join routine, yeah that's ugly too ;-)

Oh hey, that might be how to handle a Split in Blackjack...
« Last Edit: June 12, 2019, 11:51:13 am by bplus »

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: RND and RANDOMIZE information
« Reply #18 on: June 12, 2019, 11:50:38 am »
Ha! I am using variable length strings in custom type now to save small integer arrays (hands by card index) in Blackjack thread.

That's like the inverse of what I'm doing, lol.


We can do variable length strings in Type definitions now v1.3 :)

Oh, really?

I started on v1.2 and only recently upgraded to v1.3.

I guess I can clean my code up a bit!  Thanks for the tip.

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: RND and RANDOMIZE information
« Reply #19 on: June 12, 2019, 04:39:02 pm »
Didn't read your post until after I wrote mine.  That's exactly how I used QuickBASIC seeds, resetting them by saving and reusing the seed number I passed to RANDOMIZE.  How does this not work?

Not sure what you mean by "saving and reusing the seed," Raven, unless perhaps the seed was TIMER? (I have to try that, because not sure why it would work.) Or maybe you meant, save the entire pseudo-random sequence? That should be fine, but depending on your needs, it could be one huge array!

Anyway, this simple code should demonstrate the difference between the QBasic limitation of RANDOMIZE alone, as opposed to QB64's RANDOMIZE USING. Pick any seed value you want, and RANDOMIZE USING will always start from the top of the sequence.

Code: [Select]
_TITLE "Test of RANDOMIZE USING"
SCREEN _NEWIMAGE(120, 43, 0)
PRINT "First test is RANDOMIZE 2. Should see different results first and second set of numbers."
RANDOMIZE 2
PRINT RND
PRINT RND
RANDOMIZE 2
PRINT
PRINT RND
PRINT RND
PRINT
PRINT "Second test is RANDOMIZE USING 2. Should see identical results, first and second set of numbers."
RANDOMIZE USING 2
PRINT RND
PRINT RND
RANDOMIZE USING 2
PRINT
PRINT RND
PRINT RND
END
« Last Edit: June 12, 2019, 05:05:56 pm by Bert22306 »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: RND and RANDOMIZE information
« Reply #20 on: June 12, 2019, 05:23:59 pm »
Not sure what you mean by "saving and reusing the seed," Raven, unless perhaps the seed was TIMER? (I have to try that, because not sure why it would work.) Or maybe you meant, save the entire pseudo-random sequence? That should be fine, but depending on your needs, it could be one huge array!
...

The number you use for RANDOMIZE USING is called a "seed", because it grows the same random number sequence.

As in your example, your seed was 2.

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: RND and RANDOMIZE information
« Reply #21 on: June 12, 2019, 07:33:44 pm »
The number you use for RANDOMIZE USING is called a "seed", because it grows the same random number sequence.

As in your example, your seed was 2.

Of course. Are you responding to me or to Raven?

With RANDOMIZE USING, for any given seed, every time you repeat that RANDOMIZE USING in your program, the sequence starts from the beginning.

Try that with RANDOMIZE, with any given seed. It doesn't start at the beginning. It continues on where it had left off before, in your program.

Raven said that with QBasic, he saved and reused the "seed," to achieve what I just described with RANDOMIZE USING. That's what I didn't understand. Unless he meant that he saved the actual random sequence, as opposed to saving the seed.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: RND and RANDOMIZE information
« Reply #22 on: June 12, 2019, 07:37:41 pm »
Can’t you just RND(seed) to start the sequence at the start?

PRINT RND(2)
PRINT RND
PRINT RND(2)
PRINT RND

Doesn’t that repeat the cycle?  From what I remember with QB45, it’s how you set the seed — unless I’m getting old and brain damaged.  :P
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: RND and RANDOMIZE information
« Reply #23 on: June 12, 2019, 07:57:28 pm »
It has been decades since I was doing this in QB45, but I know it works, because I was reusing a fixed seed to draw my level screens.  Every time I came back to the screen, it had the same random number sequence.

Pretty sure I was just using this each time to reset it:

Code: QB64: [Select]
  1. RANDOMIZE MySeed

I'm unable to test on QB45 at the moment.  Can someone else test it?

I've never used RANDOMIZE USING before.

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: RND and RANDOMIZE information
« Reply #24 on: June 12, 2019, 08:58:57 pm »
Can’t you just RND(seed) to start the sequence at the start?

PRINT RND(2)
PRINT RND
PRINT RND(2)
PRINT RND

Doesn’t that repeat the cycle?  From what I remember with QB45, it’s how you set the seed — unless I’m getting old and brain damaged.  :P

Don't work for me none. (As they say around your parts :) )

I thought you had stumbled on some amazing new discovery, and then I would have wondered whether it also applies to QBasic. But no. It don't work. That is, yes, it shows random numbers, but no, it doesn't repeat from the top.
« Last Edit: June 12, 2019, 09:00:08 pm by Bert22306 »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: RND and RANDOMIZE information
« Reply #25 on: June 12, 2019, 10:33:47 pm »
Don't work for me none. (As they say around your parts :) )

I thought you had stumbled on some amazing new discovery, and then I would have wondered whether it also applies to QBasic. But no. It don't work. That is, yes, it shows random numbers, but no, it doesn't repeat from the top.

It’s because I left the minus out....

PRINT RND(-2)
PRINT RND
PRINT RND(-2)
PRINT RND

*******************

The number after RND is the seed, where with RND(n):
IF n < 0, use n as the set seed value
If n > 0, get the next value from the current seed set
And if 0...  I don’t remember what it’s for.  /blush


EDIT: As per the wiki (http://qb64.org/wiki/RND), a value of 0 returns the last value again.  It repeats the last result, basically.

« Last Edit: June 12, 2019, 10:36:06 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: RND and RANDOMIZE information
« Reply #26 on: June 12, 2019, 10:58:02 pm »
It’s because I left the minus out....

PRINT RND(-2)
PRINT RND
PRINT RND(-2)
PRINT RND

Wow. Cool. Did that also work in QBasic?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: RND and RANDOMIZE information
« Reply #27 on: June 12, 2019, 11:01:13 pm »
It does.  Or should, at least.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MWheatley

  • Newbie
  • Posts: 64
    • View Profile
Re: RND and RANDOMIZE information
« Reply #28 on: June 13, 2019, 07:19:16 am »
It does.  Or should, at least.  ;)

See my earlier message re: the test that you asked to be carried out in QB45.  Do you want me to re-run this code?

Malcolm