Author Topic: Why does this random pattern generator make an organized pattern over time?  (Read 9515 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #15 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.  ;)
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: Why does this random pattern generator make an organized pattern over time?
« Reply #16 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.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #17 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.  
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #18 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

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #19 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.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #20 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.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #21 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #22 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.  ;)
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: Why does this random pattern generator make an organized pattern over time?
« Reply #23 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?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #24 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...”
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #25 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.)
« Last Edit: February 10, 2021, 05:31:17 pm by Bert22306 »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #26 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.

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #27 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!
« Last Edit: February 10, 2021, 06:07:42 pm by Bert22306 »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #28 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.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #29 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