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

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
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.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
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
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
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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
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.  
« Last Edit: February 08, 2021, 09:01:28 pm by OldMoses »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
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
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
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.  

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
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.  
« Last Edit: February 08, 2021, 10:32:53 pm by Dav »

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
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!!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
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.

Offline Bert22306

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

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 #11 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
« Last Edit: February 09, 2021, 06:25:08 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline luke

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

Offline Bert22306

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

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 #14 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
You're not done when it works, you're done when it's right.