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

0 Members and 1 Guest are viewing this topic.

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 #30 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.  
« Last Edit: February 10, 2021, 09:10:43 pm by bplus »

Offline Bert22306

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

Offline luke

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

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 #33 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.  ;)
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 #34 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

   
« Last Edit: February 11, 2021, 10:56:06 am by Dav »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #35 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.
You're not done when it works, you're done when it's right.

Offline Bert22306

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

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 #37 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.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Why does this random pattern generator make an organized pattern over time?
« Reply #38 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.
Shuwatch!

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 #39 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!