Randomize patterns ?

Post Reply
Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Randomize patterns ?

Post by Operator »

While playing with block and normal distributions I noticed that using the command
RANDOMIZE created some "interference
or aliasing" patterns. Look at the attached
picture: the first and third distribution use
Randomize in each loop creating some kind
of fabric "texture" or so..., while the second
and fourth distribution don't show this at all...

Why does the use of Randomize create this
patterns ?

Any hints to understand this are welcome :D

Code: Select all

REM Randomize "patterns" ?
REM sB5.6/iOS6.1/iPhone4/by Operator
REM 
REM Program will plot 4 distrbutions
REM using 80000 points each
REM 1. Normal dist. val. & RANDOMIZE
REM 2. Normal dist. val. w/o RANDOMIZE
REM 3. Block  dist. val. & RANDOMIZE
REM 4. Block  dist. val. w/o RANDOMIZE
REM Why does the use of Randomize
REM create interference patters or 
REM a not so random distribution ?
REM How to use Randomize at all ?

GRAPHICS
GRAPHICS CLEAR 1,1,1
FILL COLOR 0,0,0
FILL ALPHA 0.1
scr_w = 320
scr_w2 = scr_w/2


FOR i = 1 TO 80000
  RANDOMIZE
  y = 5 + RND(100)
  x = rndGauss(scr_w2,60)
  FILL CIRCLE x,y SIZE 0.5
NEXT i
BEEP

FOR i = 1 TO 80000
  'RANDOMIZE
  y = 110 + RND(100)
  x = rndGauss(scr_w2,60)
  FILL CIRCLE x,y SIZE 0.5
NEXT i
BEEP

FOR i = 1 TO 80000
  RANDOMIZE
  y = 215 + RND(100)
  x = RND(scr_w)
  FILL CIRCLE x,y SIZE 0.5
NEXT i
BEEP

FOR i = 1 TO 80000
  'RANDOMIZE
  y = 320 + RND(100)
  x = RND(scr_w)
  FILL CIRCLE x,y SIZE 0.5
NEXT i
BEEP
BEEP

END




DEF rndGauss(mean,stdDev)
'Standard NORMAL variate using 
'Marsaglia polar method, code ported
'out of Wikipedia

WHILE q <= 0 OR q >= 1
  u = 2 * RND(1) - 1
  v = 2 * RND(1) - 1
  q = u * u + v * v
END WHILE
p = SQR(-2 * LOG(q)/q)

'reset q value for next call
q = 0

RETURN mean + stdDev * p * u
END DEF
Attachments
image.jpg
image.jpg (288.26 KiB) Viewed 1413 times

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Randomize patterns ?

Post by Mr. Kibernetik »

RANDOMIZE is not supposed to be called in a loop. Its purpose is to be called only once to change a random sequence.
In BASIC the random function returns the same random sequence each run until it is changed with RANDOMIZE command.

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Randomize patterns ?

Post by Henko »

Operator wrote:While playing with block and normal distributions I noticed that using the command
RANDOMIZE created some "interference
or aliasing" patterns. Look at the attached
picture: the first and third distribution use
Randomize in each loop creating some kind
of fabric "texture" or so..., while the second
and fourth distribution don't show this at all...

Why does the use of Randomize create this
patterns ?

Any hints to understand this are welcome :D
I'm not sure, but i can guess why:
The randomize statement often uses an algorithm based on the internal system time and date to ensure that the starting point for rnd() is always different. If used in a simple loop, the date/time samples for the randomize statement are incredible close to each other and on top of that equidistant. I can imagine that the algorithm cannot cope with that situation to generate complete random starting points.
Just guessing... :?

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Randomize patterns ?

Post by Mr. Kibernetik »

Yes, Henko is right. This is the case.

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Randomize patterns ?

Post by Operator »

@Mr. Kibernetik, @Henko
Thanks for your feedback and explanations,
now aware of not using Randomize in a loop,
just if some kind of texture is desired.

Post Reply