Randomness

Everything else that doesn't fall into one of the other PB categories.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Randomness

Post 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 :)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Randomness

Post 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
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post 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.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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).
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post 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]
regards,
benny!
-
pe0ple ar3 str4nge!!!
Post Reply