QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Dav on February 08, 2021, 07:44:00 pm

Title: Why does this random pattern generator make an organized pattern over time?
Post by: Dav on February 08, 2021, 07:44:00 pm
This is more of a math/randomize question I suppose.  I didn't go very far in math, so if someone could explain this to me in a layman way why this occurs, I'll be thanking you.

I am playing around making what I think are random pattern makers, but over time this creates an organized pattern on the screen.  Why is that?

Run this for a little while, and slowly it makes organized stripes on the screen.  Shouldn't it remain a random mess?

- Dav

 
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3.  
  4.     FOR l = 1 TO 1000
  5.         x = RND * 600: y = RND * 600: c = RND * 255
  6.         LINE (x, y)-(x + 20, y + 20), _RGBA(c, c, c, RND * 1), BF
  7.     NEXT
  8.     _LIMIT 100
  9.  
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Pete on February 08, 2021, 07:57:28 pm
Did you try this trick, I think Mark posted awhile back?

x = RND(-TIMER)  * 600: y = RND(-TIMER)  * 600: c = RND(-TIMER)  * 255

As to the why part, no clue.

Pete
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 08, 2021, 08:20:21 pm
Remember:  QB64 doesn’t have any true random numbers.  We get values from a pseudo random formula, and that formula repeats every 16,000,000 cycles or so.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: OldMoses on February 08, 2021, 08:58:46 pm
Here's a modification that seems to mix it up a bit better. Although I'm sure it's still repeating a sequence as it seems to stop after a while anyway.

Funny thing about this, when I just put the RND in lieu of a,b & d as an intermediary, I get a very different result.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3.  
  4.     FOR l = 1 TO 1000
  5.         RANDOMIZE RND * l
  6.         a = RND
  7.         b = RND
  8.         d = RND
  9.         x = a * 600: y = b * 600: c = d * 255
  10.         LINE (x, y)-(x + 20, y + 20), _RGBA(c, c, c, RND * 1), BF
  11.     NEXT
  12.     _LIMIT 100
  13.  
  14.  
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 08, 2021, 09:27:15 pm
DIM SHARED Array(1000000) AS _FLOAT





FUNCTION Rand
    STATIC Init, IndexOn
    IF Init = 0 THEN
        Init = -1
        FOR I = 0 TO 1000000
            Array(I) = I / 1000001
        NEXT
    END IF
    IF IndexOn = 0 THEN
         RANDOMIZE TIMER
         FOR I = 0 TO 1000000
               SWAP Array(I), Array (INT(RND * 1000001)
        NEXT
     END IF
     IndexOn = (IndexOn + 1) MOD 1000000
     Rand = Array(IndexOn)
END FUNCTION



Something like the above should make it rather hard to find repeating patterns, as you’re constantly using and then shuffling an array of 1000001 elements.  You might give something similar to it a try, for a different random method.

Note: Not actually tested as I’m on my iPad at Mom’s (thus the lack of being in a code box), but the concept should come across, I’m hoping.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: STxAxTIC on February 08, 2021, 09:43:22 pm
For once, I *actually* think its the pseudo-generator that is responsible for this. I see enough confusion centered around RND that is *not* this issue, but for once, I actually think so...

Anyway, the shortest answer is to re-seed often, like once per main loop. I added one line of code:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3.  
  4.     FOR l = 1 TO 1000
  5.         x = RND * 600: y = RND * 600: c = RND * 255
  6.         LINE (x, y)-(x + 20, y + 20), _RGBA(c, c, c, RND * 1), BF
  7.     NEXT
  8.     _LIMIT 100
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: bplus on February 08, 2021, 10:06:04 pm
How about blanking fading out the old:
Code: QB64: [Select]
  1. Screen _NewImage(600, 600, 32)
  2.  
  3.  
  4.     For i = 1 To 1000
  5.         x = Rnd * 600: y = Rnd * 600: c = Rnd * 255
  6.         Line (x, y)-(x + 20, y + 20), _RGBA(c, c, c, Rnd * 20), BF
  7.     Next
  8.     Line (0, 0)-(_Width, _Height), &H10000000, BF
  9.     _Limit 100
  10.  
  11.  
  12.  
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Dav on February 08, 2021, 10:29:28 pm
Thank you all for your quick answers and workarounds!  I went to check on my dad a little while, and came back to find several answers.   All of them are helpful.  So I guess it's a visual representation of the random formula, and the best way is avoid a pattern is to re-seed often?  I like how quick a fix RANDOMIZE RND will do it. Thanks.

A while back I was looking for a quick way to do a wind/scrubbing effect for something else, and @bplus your code up there finally got me there. Thanks.

- Dav

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3.  
  4.     FOR i = 1 TO 1000
  5.         x = RND * 600: y = RND * 600
  6.         LINE (x, y)-(x + RND * 200, y + RND * 20), _RGBA(RND * 255, RND * 255, RND * 255, RND * 20), BF
  7.     NEXT
  8.     LINE (0, 0)-(_WIDTH, _HEIGHT), &H10000000, BF
  9.     _LIMIT 100
  10.  
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 09, 2021, 04:52:17 pm
Naw, I think we're going on the wrong track. First off, try to reseed often, within the loop, and the same pattern soon occurs.

The problem has nothing to do with the PRNG. It has everything to do with the fact that Rnd creates numbers in the range 0 to <1. An then, you are adding 20 to that random number.

Well, 20 + Rnd will hardly be all over the map!!
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: luke on February 09, 2021, 05:27:45 pm
Naw, I think we're going on the wrong track. First off, try to reseed often, within the loop, and the same pattern soon occurs.

The problem has nothing to do with the PRNG. It has everything to do with the fact that Rnd creates numbers in the range 0 to <1. An then, you are adding 20 to that random number.

Well, 20 + Rnd will hardly be all over the map!!
I disagree with the answer in its entirety.

Reseeding a random number generator with numbers from that generator is really Not A Good Idea. Keep in mind reseeding merely reads numbers from a different position in the sequence and does not change the order of the sequence, and if you're reseeding with numbers from the generator itself you're really just reading the sequence in a non-linear fashion.

Your options for better randomness are:
 - Implement your own random number generator. Algorithms for such are available on the internet and should be able to give decent performance with not too large an implementation.
 - Ask elsewhere for random numbers. On Linux & Mac OS random numbers can be had by reading the file /dev/urandom. On Windows I believe @SpriggsySpriggs has an API call that provides random numbers.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 09, 2021, 05:40:02 pm
I disagree with the answer in its entirety.

Reseeding a random number generator with numbers from that generator is really Not A Good Idea.

Did you bother to read my response, Luke? I said, do try to reseed, and the same pattern occurs.

Edit: on re-read of Dav's original program, he does multiply Rnd by large numbers initially. But then, adds this constant bias of 20. So my initial response was not quite right, but still, it's not the PRNG causing the problem.

Quote
Your options for better randomness are:
 - Implement your own random number generator. Algorithms for such are available on the internet and should be able to give decent performance with not too large an implementation.

And that will not solve the problem, if you continue to add a constant bias of 20, to draw the lines.

I've provided two programs to test the reasonable actual results of the built-in PRNG. It's not that bad, certainly not at drawing random dots of a flat screen. Try it. Works pretty darned well.

Code: [Select]
Rem  Program creates dots using random x and y coordinates
Rem -------
Do
    Call Initialize
    Call DrawDots
Loop
End

Sub Initialize
    _Title "Random Number Distribution"
    Screen _NewImage(840, 480, 12)
    Cls
End Sub

Sub DrawDots
    Randomize Timer
    Color 9
    Print "First random number this cycle"; Rnd
    Do
        x = Rnd * 840
        y = Rnd * 480
        Circle (x, y), 1, 14
        _Delay .005
        If InKey$ = Chr$(27) Then Exit Do
    Loop
    Cls
    Locate 15, 35
    Input "Start with new seed (y/n)"; cont$
    If cont$ = "n" Then End
End Sub
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: STxAxTIC on February 09, 2021, 06:17:34 pm
Not to talk past both you guys, because Luke is right - but if you wanna stay within the vanilla ecosystem, I noticed you can make the patterns go away by just tossing out a few RNDs every time you need one:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3.  
  4.     FOR l = 1 TO 1000
  5.         x = rng * 600: y = rng * 600: c = rng * 255
  6.         LINE (x, y)-(x + 20, y + 20), _RGBA(c, c, c, rng * 1), BF
  7.     NEXT
  8.     _LIMIT 100
  9.  
  10.     FOR k = 1 TO INT(RND * 4)
  11.         j = RND
  12.     NEXT
  13.     rng = RND
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: luke on February 09, 2021, 06:19:57 pm
It seems I misread your message.

In that case I disagree with the second half of your message.

The limitation of the random number generator is not at all due to it returning a number between 0 and 1. The number is the result of rnd_seed/0x1000000 where rnd_seed is a a 32 bit integer and a SINGLE has 24 bits in just the significand alone (not including the exponent) so there's plenty of bits to hold a good distribution.

And just in case you thought the built-in random number generator was good at putting dots on the screen, here's a program that says otherwise:
Code: [Select]
Screen 12

Do
    x = Rnd * _Width
    y = Rnd * _Height
    c = Rnd * 16
    q = Rnd
    PSet (x, y), c
    _Limit 10000
Loop
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 09, 2021, 06:33:56 pm
Weird, you do get a pattern.

But not if you run it modified like this:

Code: [Select]
Screen 12

Do
    x = Rnd * _Width
    y = Rnd * _Height
    Circle (x, y), 1, 15
    Rem     c = Rnd * 16
    Rem     q = Rnd
    Rem     PSet (x, y), c
    _Limit 10000
Loop
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: STxAxTIC on February 09, 2021, 06:53:10 pm
That's not really an apples-to-apples comparison Bert. Here's Luke's program with every instance of RND replaced with "rng". Makes no pattern.

Code: QB64: [Select]
  1.  
  2.     x = rng * _WIDTH
  3.     y = rng * _HEIGHT
  4.     c = rng * 16
  5.     q = rng
  6.     PSET (x, y), c
  7.     _LIMIT 10000
  8.  
  9.     FOR k = 1 TO INT(RND * 4)
  10.         j = RND
  11.     NEXT
  12.     rng = RND
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 09, 2021, 07:03:47 pm
Code: QB64: [Select]
  1. DIM SHARED Array(1000000) AS _FLOAT
  2.  
  3.  
  4.  
  5.  
  6.  
  7.     x = Rand * _WIDTH
  8.     y = Rand * _HEIGHT
  9.     c = Rand * 16
  10.     q = Rand
  11.     PSET (x, y), c
  12.     ' _LIMIT 10000
  13.  
  14.  
  15.  
  16.  
  17.     STATIC Init, IndexOn
  18.     IF Init = 0 THEN
  19.         Init = -1
  20.         FOR I = 0 TO 1000000
  21.             Array(I) = I / 1000001
  22.         NEXT
  23.     END IF
  24.     IF IndexOn = 0 THEN
  25.         RANDOMIZE TIMER
  26.         FOR I = 0 TO 1000000
  27.             SWAP Array(I), Array(INT(RND * 1000001))
  28.         NEXT
  29.     END IF
  30.     IndexOn = (IndexOn + 1) MOD 1000000
  31.     Rand = Array(IndexOn)
  32.  

As I said above, simply shuffling a large array and using it to generate random values, usually works more than good enough to suit my needs.  It certainly creates a good "pattern" of static above.  ;)
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: bplus on February 09, 2021, 07:19:53 pm
Pattern removed
Code: QB64: [Select]
  1.  
  2.     x = Rnd * _Width
  3.     y = Rnd * _Height
  4.     c = Rnd * 16
  5.     PSet (x, y), c
  6.     _Limit 10000
  7.  
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 09, 2021, 07:26:43 pm
This is more of a math/randomize question I suppose.  I didn't go very far in math, so if someone could explain this to me in a layman way why this occurs, I'll be thanking you.

I am playing around making what I think are random pattern makers, but over time this creates an organized pattern on the screen.  Why is that?

Run this for a little while, and slowly it makes organized stripes on the screen.  Shouldn't it remain a random mess?

- Dav

 
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3.  
  4.     FOR l = 1 TO 1000
  5.         x = RND * 600: y = RND * 600: c = RND * 255
  6.         LINE (x, y)-(x + 20, y + 20), _RGBA(c, c, c, RND * 1), BF
  7.     NEXT
  8.     _LIMIT 100
  9.  

One more point of interest here -- as written, this will eventually turn the whole screen a solid gray, simple because you're gradually increasing alpha levels.   (After the first pass, you've blended a gray tone onto the screen, and you just continue to blend onto that already blended screen...)

Code: QB64: [Select]
  1.  
  2. DIM SHARED Array(1000000) AS _FLOAT
  3. SCREEN _NEWIMAGE(600, 600, 32)
  4.  
  5.  
  6.     FOR l = 1 TO 1000
  7.         x = Rand * 600: y = Rand * 600: c = Rand * 255
  8.         LINE (x, y)-(x + 20, y + 20), _RGBA(c, c, c, Rand * 1), BF
  9.     NEXT
  10.     _LIMIT 120
  11.  
  12.     STATIC Init, IndexOn
  13.     IF Init = 0 THEN
  14.         Init = -1
  15.         FOR I = 0 TO 1000000
  16.             Array(I) = I / 1000001
  17.         NEXT
  18.     END IF
  19.     IF IndexOn = 0 THEN
  20.         RANDOMIZE TIMER
  21.         FOR I = 0 TO 1000000
  22.             SWAP Array(I), Array(INT(RND * 1000001))
  23.         NEXT
  24.     END IF
  25.     IndexOn = (IndexOn + 1) MOD 1000000
  26.     Rand = Array(IndexOn)
  27.  
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Dav on February 09, 2021, 07:44:22 pm
Yes, I just posted that as is was because it showed the curious pattern.  I was getting it on several pattern makers that used colors too, and was very curious as to why.  Your swap array does prevent the pattern.

- Dav
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 09, 2021, 09:06:24 pm
Your swap array does prevent the pattern.

I won’t swear that swapping an array like that won’t eventually end up repeating, but if it does, the number of iterations is more than I’ve rand across in my personal applications. 

After all, how many times do you need to shuffle a deck of 52 cards, before you get it back into perfect order?  Eventually, I imagine it’ll happen, but at what point? 

And this is an array of 1000001 elements.  I’d imagine it’d take a lot more shuffling than a deck of cards, before you end up back at the starting point once again.

If it’s not “true random”, it’s close enough of a method for all my needs.  ;)
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Dimster on February 10, 2021, 09:56:48 am
So, I'm not exactly sure of the definition of "True Random". It would seem to me that repeat numbers HAVE to be as a result of the size of a SET of numbers. No matter what the Set size is, eventually you would have to accept a repeat of a number or group of numbers. If set size dictates repeats and True Randomness excludes a repeat of a number then either the set size has to be infinity or the Random function must be set to cease/end at the first repeated number.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: bplus on February 10, 2021, 10:59:34 am
So, I'm not exactly sure of the definition of "True Random". It would seem to me that repeat numbers HAVE to be as a result of the size of a SET of numbers. No matter what the Set size is, eventually you would have to accept a repeat of a number or group of numbers. If set size dictates repeats and True Randomness excludes a repeat of a number then either the set size has to be infinity or the Random function must be set to cease/end at the first repeated number.

True random could repeat numbers million times, but not usually.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 10, 2021, 12:35:27 pm
True random could repeat numbers million times, but not usually.

Aye, and that’s the one big drawback to shuffling an array like I illustrated above.  Imagine it as playing cards  — you don’t reshuffle the deck until ALL cards are played.  If you’re a card counter, you realize that once the Ace of Spades has been played, it’s not going to be dealt again until all the other cards are played first.  The bigger the array, the harder it is to count, but it’s very obvious if you use such a method just for coin tosses.  If the first toss is heads, the second toss is tails, and vice-versa.

Array shuffling is a pseudorandom method that isn’t useable for all purposes, but it’s useful for non-repeating patterns like we’re discussing here.  ;)
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: bplus on February 10, 2021, 12:51:56 pm
Just shuffle your deck between every call, too bad for card counters ;-))

This makes is possible to have a million numbers in a row?
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 10, 2021, 01:01:30 pm
Just shuffle your deck between every call, too bad for card counters ;-))

You can do that, but then there’s the hit on performance with shuffling so constantly.  You’re not just saying, “Quick, give me a random number so I can decide if the ghost goes left or right after Pac-man!”  Instead, you’re saying, “Quick, shuffle a million element array and then give me a number...”
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 10, 2021, 05:22:33 pm
Well, okay, getting away from Steve's highjacking of the thread(!), and back to these odd patterns people are experiencing, I went back to Luke's example. Yes, I got patterns again. But the question is, what causes that?

So I wrote this almost identical program, except using Screen _NewImage (which has no effect), then doing nothing more than either:

(a) eliminate the one, useless, q = Rnd, or

(b) define that one useless variable, but undo the random color variable used in Pset (just use white pixels), or

(c) copy what Luke did, but define another extra s = Rnd variable,

and you see no pattern. You'd think, if this PRNG is so bad, I'd see a patterns with these other examples too, no?

In the code here, you should see patterns. But, uncomment one, or both, of the Rem statements, or add another few useless random variables, and the pattern goes away. Really, try it.

Code: [Select]
Screen _NewImage(840, 480, 12)
Randomize Timer
Do
    x = Rnd * _Width
    y = Rnd * _Height
    c = Rnd * 16
    q = Rnd
    Rem    r = Rnd
    Rem    s = Rnd
    PSet (x, y), c
    If InKey$ = Chr$(27) Then End
    _Limit 10000
Loop
End

(Edited only to correct typos.)
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: bplus on February 10, 2021, 05:31:48 pm
Quote
(a) eliminate the one, useless, q = Rnd, or

as I posted ;)

Random number generator patterns are susceptible to 2's (read in wiki) when having one of these discussions on another forum. Notice Luke's q makes the number of calls even every cycle.

wiki suggests for heads tails if rnd repeats last head or tail to take the next flip as the official one, something like that.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 10, 2021, 05:57:27 pm
Random number generator patterns are susceptible to 2's (read in wiki) when having one of these discussions on another forum. Notice Luke's q makes the number of calls even every cycle.

wiki suggests for heads tails if rnd repeats last head or tail to take the next flip as the official one, something like that.

Well, sumbich, you're exactly right, bplus. I entered a few extra useless variables, to total 8, and got a similar pattern.

So, bottom line, when using Rnd in a loop, make sure the total number of variables which use Rnd is not a power of 2. If it is a power of 2, add one dummy random variable, in that loop.

Thanks for that info!
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 10, 2021, 06:33:59 pm
I’d think even odd values would repeat; just at different intervals.

Let’s say I have a list of 4 numbers, from 1 to 4.

Now, I get records in sequence of 4:

(1, 2, 3, 4) (1, 2, 3, 4) (1, 2, 3, 4)...  these repeat instantly!

Now, let’s get records in sequences of 3:

(1, 2, 3) (4, 1, 2) (3, 4, 1) (2, 3, 4), (1, 2, 3)....repeat.

It’s not that your sequence of 3 won’t repeat — it’ll just take the LCD (Lowest Common Denominator) times for it to repeat...

Which is what you guys are producing with your tests.  ;)
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 10, 2021, 07:56:28 pm
I have to ponder on your suggestion, Steve.

In the meantime, here's demo, split screen, where the left side sees the looping Rnd variables to be non-power-of two, while the right side sees 8 Rnd variables in the loop.

But puzzle me this: Why does the right side appear to be a lighter color, overall? If you use _Height and _Width, to size the screen, the result looks darker, than if you use the actual pixel count. Rewrite the right side, using

z = (_Width / 2) + (420 * Rnd)

and you will see both sides equally dark. Weird.

Code: [Select]
Screen _NewImage(840, 480, 12)
Rem -------
Rem  Moral of the story: make sure *not* to define a power of 2, as total
Rem  number of random variables in the loop. Test this with 4 or 8 Rnd
Rem  variables, and you sill see diagonal patterns in the image.
Rem -------
Randomize Timer
Do
    x = Rnd * _Width
    y = Rnd * _Height
    z = 420 + Rnd * 420
    w = Rnd * _Height
    c = 1 + Int(Rnd * 15)
    q = Rnd
    r = Rnd
    s = Rnd
    Rem    t = Rnd
    Rem    u = Rnd
    PSet (x, y), c
    PSet (z, w), c
    If InKey$ = Chr$(27) Then End
    _Limit 10000
Loop
End
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: bplus on February 10, 2021, 08:56:43 pm
I have to ponder on your suggestion, Steve.

In the meantime, here's demo, split screen, where the left side sees the looping Rnd variables to be non-power-of two, while the right side sees 8 Rnd variables in the loop.

But puzzle me this: Why does the right side appear to be a lighter color, overall? If you use _Height and _Width, to size the screen, the result looks darker, than if you use the actual pixel count. Rewrite the right side, using

z = (_Width / 2) + (420 * Rnd)

and you will see both sides equally dark. Weird.

Code: [Select]
Screen _NewImage(840, 480, 12)
Rem -------
Rem  Moral of the story: make sure *not* to define a power of 2, as total
Rem  number of random variables in the loop. Test this with 4 or 8 Rnd
Rem  variables, and you sill see diagonal patterns in the image.
Rem -------
Randomize Timer
Do
    x = Rnd * _Width
    y = Rnd * _Height
    z = 420 + Rnd * 420
    w = Rnd * _Height
    c = 1 + Int(Rnd * 15)
    q = Rnd
    r = Rnd
    s = Rnd
    Rem    t = Rnd
    Rem    u = Rnd
    PSet (x, y), c
    PSet (z, w), c
    If InKey$ = Chr$(27) Then End
    _Limit 10000
Loop
End

You have x over the whole width and z over 2nd half, so 2nd half gets more, about 1/2 of x was supposed to get.

Pete would tell you it's a half-width idea ;-))

fixed
Code: QB64: [Select]
  1. Screen _NewImage(840, 480, 12)
  2. Rem -------
  3. Rem  Moral of the story: make sure *not* to define a power of 2, as total
  4. Rem  number of random variables in the loop. Test this with 4 or 8 Rnd
  5. Rem  variables, and you sill see diagonal patterns in the image.
  6. Rem -------
  7.     x = Rnd * _Width / 2
  8.     y = Rnd * _Height
  9.     z = 420 + Rnd * 420
  10.     w = Rnd * _Height
  11.     c = 1 + Int(Rnd * 15)
  12.     q = Rnd
  13.     r = Rnd
  14.     s = Rnd
  15.     Rem    t = Rnd
  16.     Rem    u = Rnd
  17.     PSet (x, y), c
  18.     PSet (z, w), c
  19.     If InKey$ = Chr$(27) Then End
  20.     _Limit 10000
  21.  
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 10, 2021, 09:30:37 pm
You have x over the whole width and z over 2nd half, so 2nd half gets more, about 1/2 of x was supposed to get.

Pete would tell you it's a half-width idea ;-))

Damn, right again. Funny thing is, in my working copy, I had already fixed that. Looks like I made too many changes at the same time, and lost track of what fixed what. Thanks again, bplus!

P.S. You get a particularly beautiful color pattern, on the right side, if you define 16 random variables total. Seems brighter than with 8 variables. (Then again, not exactly what you want to see, in a PRNG test, eh?)
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: luke on February 11, 2021, 01:05:26 am
I feel that perhaps the point I was trying to make was too subtle.

In particular, I was not saying anything about how to draw dots on a screen.

The point (pardon the pun) is that the random number generator has some very undesirable behaviours, namely a short period and non-uniform distribution.

The dots in screen was just a convenient way to show how damning the issue can be.

Making the on-screen pattern go away by changing which random numbers you choose is not fixing the problem, it's just hiding out from your eyes.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 11, 2021, 01:23:12 am
Here's the perfect proof of repetition:

Code: QB64: [Select]
  1. REDIM Array(100000000) AS SINGLE
  2. Array(0) = RND
  3. PRINT "START ", 0
  4.  
  5.     count = count + 1
  6.     Array(count) = RND
  7.     IF Array(count) = Array(0) THEN
  8.         PRINT "REPEAT ", count
  9.         repeat = repeat + 1
  10.     END IF
  11. LOOP UNTIL repeat = 4
  12.  
  13.     INPUT "Give me a number from 0 to 16777215 =>", x&
  14.     PRINT Array(x&), Array(x& + 16777216), Array(x& + 16777216 * 2), Array(x& + 16777216 * 3)
  15. LOOP UNTIL x& = 0
  16.  
  17.  

Every 16, 777, 216 numbers repeats.  That's 2^24, and it explains why it's so easy to replicate with "powers of 2", as you guys were looking at.  It'll repeat with other values as well, but it's just not as obvious plotting it, as I tried to show you with a minimal 4-number repeating data set.  ;)
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Dav on February 11, 2021, 10:24:52 am
I've been reading all the replies here.  Haven't responded much because I don't have anything worth adding - but I really appreciate everyones input so I'll reply with thanks.  My question has been answered.

Being cursed with curiosity I started trying to code getting a random number without using RND or external library functions.  Wow - I sure have been taking RND for granted.   I'm reading up on pseudo random number algorithms.  Way above my head right now.  Much respect to you who swim in those deep waters.  There is something in QB64's nature that always *seemed* random, the starting window position.  Maybe can use it as a random seed number.  RandomSeedNum& = _SCREENX * _SCREENY * TIMER.  That's about all this math-challenged guy can think of right now for getting one random number without RND.   

- Dav

   
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: STxAxTIC on February 11, 2021, 10:50:00 am
This is gold!

Quote
There is something in QB64's nature that always *seemed* random, the starting window position.  Maybe can use it as a random seed number.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: Bert22306 on February 11, 2021, 05:37:10 pm
Every 16, 777, 216 numbers repeats.  That's 2^24, and it explains why it's so easy to replicate with "powers of 2", as you guys were looking at.  It'll repeat with other values as well, but it's just not as obvious plotting it, as I tried to show you with a minimal 4-number repeating data set.  ;)

This limitation, number of bits in the seed, applies to every PRNG. Yes, 24 bits is a small value.

When I started looking into the QB PRNG, not knowing how useful it was, I did increase the seed number, until it made no difference in the generated sequence of pseudo-random numbers. As I recall, it looked to me like it was only 22 bits wide, in effect. So, for sure, you have to take this into account, whenever PRNGs are used.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: bplus on February 11, 2021, 07:49:20 pm
This 2 thing for random numbers might come from fact that 2 is most used lowest prime number factoring the most numbers.

Only one even prime and yet it factors 1/2 all numbers >= sum of all the numbers with odd prime as lowest factor.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SpriggsySpriggs on February 11, 2021, 07:50:26 pm
WinAPI has a function called CryptGenRandom, I think. It can be used to make some nice random numbers. I have code for it somewhere.
Title: Re: Why does this random pattern generator make an organized pattern over time?
Post by: SMcNeill on February 11, 2021, 08:28:35 pm
This 2 thing for random numbers might come from fact that 2 is most used lowest prime number factoring the most numbers.

Only one even prime and yet it factors 1/2 all numbers >= sum of all the numbers with odd prime as lowest factor.

The “2 thing” is just because you have a repeating list in a value of a power of 2.

Here’s a question: With a list of 16 repeating values, how many do you have to draw to repeat the list perfectly, if you draw 1 at a time?  2 at a time?  3 at a time?  N at a time?

If you draw one at a time, after 16 numbers, you repeat perfectly. (0123456789ABCDEF.... 0123456789ABCDEF...)

If you draw 2 at a time, after 16 numbers, you repeat perfectly.  (01,23,45,67,89,AB,CD,EF...  01,23,45...)

If you draw 3 at a time, it takes 48 numbers to repeat your original pattern perfectly. (012,345,678,9AB,CDE,F01... 234,567,89A,BCD,EF0... 123,456,789,ABC, DEF... 012,345,678...)

Your repeat interval is the Lowest Common Denominator of the number you’re drawing and the number in your sequence.  It’s more obvious when dealing with this “2 thing” because you have the smallest possible repetition iteration, but it’s going to end up repeating perfectly no matter what, in the end.