Page 1 of 1

Randomness

Posted: Thu Jun 17, 2004 11:06 pm
by tinman
I used the following code (obviously, not complete, window and windowedscreen set up, but RandomSeed NOT called - Random() is always supposed to be randomised to the timer (I guess - or maybe something else) each time the program is run).

Anyway, the colours of pixels are random, but is it just me or is there a visible pattern to the data (looks like it's offset, as if the random numbers are repeating very frequently)? Perhaps someone who understands random number generation can explain why, but even if there is a valid reason, isn't this a bit....poor?

Code: Select all

                StartDrawing(ScreenOutput())
                Line(0,0,*ws\ws_ScreenWidth,0,RGB(240,0,0))
                Line(0,*ws\ws_ScreenHeight-1,*ws\ws_ScreenWidth,0,RGB(240,0,0))
                
                For dots.l=1 To 50000
                    Plot(Random(*ws\ws_ScreenWidth), 1 + Random(*ws\ws_ScreenHeight - 2), RGB(Random(255), Random(255), Random(255)))
                Next

Image

Image


Edit: remove extra /img tag

Edit2: Oh, yeah, and I'm not conviced by the fact that the red lines at the top and bottom of my screen have been overwritten. Might just be my code being crap though :)

PS, use some sort of screen magnifier to see the pixels :)

Re: Randomness

Posted: Fri Jun 18, 2004 4:38 am
by NoahPhense
Someone will be able to give you a real explaination, but for now. Try this:

Code: Select all

RandomSeed(Random(255)); any number you want in here 

a=Random(255)
b=Random(255)
c=Random(255)

Debug a
Debug b
Debug c

End
I do know that if I did not have that RandomSeed in there, then EVERY
time I run my standard Random's, I would get my "random" numbers..
and then next time I ran them, they would be 'random' but in the same
order.. ;) if that makes sense..

So by using the RandomSeed() ::

Description

Changes the actual seed for random number returned with Random().

Note: each times a program start, a new seed is generated, so RandomSeed() is only useful when the programmer want to get always the same random number in the same order.


Now mind you, I've twisted it up a bit, by throwing Random() inside the
RandomSeed() .. ;)


- np

Posted: Fri Jun 18, 2004 9:05 am
by dmoc
Pattern suggests *ws\ws_ScreenWidth could be wrong. A hostogram would be better to show how random PB's random number generator is.

Posted: Fri Jun 18, 2004 9:29 am
by tinman
@NoahPhense: Yeah, I know how RandomSeed() works (or should I say, what it does), and the description says you shouldn't need it. So I never used it. For more random numbers than you suggest, try RandomSeed(GetTickCount_()) or similar.

@dmoc: The screenwidth shouldn't have an effect - plotting is clipped if you go off the screen (AFAIR).

Posted: Fri Jun 18, 2004 9:35 am
by benny
@Tinman:

Hmm ... maybe a

Code: Select all

RandomSeed(timeGetTime_())
before the for/next-loop helps ? Don't know :roll: :?: :!:

[EDIT] ... argh ... too late ... sorry [/EDIT]