Rand.Int Not so random...
Author |
Message |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Thu Nov 27, 2008 3:28 pm Post subject: Rand.Int Not so random... |
|
|
well I was just folling around with turing trying to make an explosion then I saw what Was happening with the circles. Anyways then I stretched it out and was given this.
This code proves that Rand.Int is not really Random. The computer will acually sort the colors! lol
Anyways for anyone who doesn't belive me heres the code.
code: | View.Set ("Graphics:max,max")
var x1,x2,y1,y2,clr : int
var random: int
procedure explode
for i:1..10
random:= Rand.Int (1,6)
if random = 1 then
clr:= 12
elsif random = 2 then
clr:= yellow
elsif random = 3 then
clr:= 42
elsif random = 4 then
clr:= 43
elsif random = 5 then
clr:= 41
elsif random = 6 then
clr:= 40
end if
x1:= Rand.Int (i,55)
x2:= Rand.Int (0,25)
y1:= Rand.Int (i,55)
y2:= Rand.Int (0,25)
Draw.FillOval ((x1+100)* random,(y1)* random,(x2)div random,(y2+100)div random,clr)
Draw.FillOval ((x1+100)* random,(y1)* random,(x2)div random,(y2+100)div random,clr)
View.Update
delay(0)
end for
View.Update
end explode
loop
explode
View.Update
end loop |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
WaluiJ
|
Posted: Thu Nov 27, 2008 4:13 pm Post subject: Re: Rand.Int Not so random... |
|
|
It's because the coodinates of the circles include the random variable in it, so all the yellow colored ones would be about the same coodinates.
Here's what it should be.
code: |
View.Set ("Graphics:max,max")
var x1,x2,y1,y2,clr : int
var random: int
loop
for i:1..10
random:= Rand.Int (1,6)
if random = 1 then
clr:= 12
elsif random = 2 then
clr:= yellow
elsif random = 3 then
clr:= 42
elsif random = 4 then
clr:= 43
elsif random = 5 then
clr:= 41
elsif random = 6 then
clr:= 40
end if
x1:= Rand.Int (0,maxx)
x2:= Rand.Int (0,25)
y1:= Rand.Int (i,55)
y2:= Rand.Int (0,25)
Draw.FillOval ((x1),(y1),(x2),(y2+100),clr)
Draw.FillOval ((x1),(y1),(x2),(y2+100),clr)
View.Update
delay(0)
end for
View.Update
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Nov 27, 2008 5:17 pm Post subject: RE:Rand.Int Not so random... |
|
|
You are modifying X and Y values based on 'random', which is the same variable used to initialize 'clr'. Thus, they will form a pattern. |
|
|
|
|
![](images/spacer.gif) |
DanielG
|
Posted: Thu Nov 27, 2008 9:09 pm Post subject: RE:Rand.Int Not so random... |
|
|
although turing (and in fact all) random numbers are not truly, they are not usually this bad. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu Nov 27, 2008 9:30 pm Post subject: RE:Rand.Int Not so random... |
|
|
Daniel, this isn't 'bad' randomness, it is programming designed to fool you into thinking it is random, by clever (or perhaps mistaken) manipulation of variables. |
|
|
|
|
![](images/spacer.gif) |
copthesaint
![](http://compsci.ca/v3/uploads/user_avatars/15853548854c9c056fda48d.jpg)
|
Posted: Thu Nov 27, 2008 11:02 pm Post subject: RE:Rand.Int Not so random... |
|
|
:p; Insectoid Knows it's an illusion p;
:How it works:
1st look at the situations for coloring: the if statmets.
2nd look at the Draw.FillOval. Notice that it's X and Y values are multiplied by The same var that changes color.
Result When the color is red It stays closest to the bottom left when it is for exmple orange the oval is further away from the bottom left corner.
This effect would not work without the variable random. Thry increasing it's greatest value from 6 to maybe 7 and watch the effect.
But The fact still stays and remember Rand.Int is based on time (sorry I had To see If anyone else would understand how this works^^) |
|
|
|
|
![](images/spacer.gif) |
Dan
![](http://wiki.compsci.ca/images/archive/3/3c/20100325043407!Danspic.gif)
|
Posted: Thu Nov 27, 2008 11:03 pm Post subject: RE:Rand.Int Not so random... |
|
|
With out seeing the source code to truing i can't be sure (at least with out some effort) but i blive that turing uses a standard pesdorandom algortham for geting random numbers.
If thats the case the random numbers should be fiarly well disbutited between 0 and 1 (or for rand.int between the two parama).
To test this you can run a program like this:
Turing: |
var ints : array 1 .. 10 of int
var runs : int := 10000
for k : 1 .. 10
ints (k) := 0
end for
for i : 1 .. runs
ints (Rand.Int (1, 10)) += 1
end for
for j : 1 .. 10
put ints (j)
end for
|
If you run this you can see the number the time each number is picked is about the same, if you realy wanted you could try runing some static functions on it but it is prity clear it is close to an even disbution betwene the numbers.
I deftaly would not use Rand.int for security or anything that some ones life or money is ridding on but it would be equilvent to the basic random functions in most langues.
If you are realy woried you can look into Rand.Seed and Rand.Reset for messing with what set of random numbers the pesdorandom algorithm puts out. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Fri Nov 28, 2008 12:15 am Post subject: Re: RE:Rand.Int Not so random... |
|
|
copthesaint @ Thu Nov 27, 2008 11:02 pm wrote: remember Rand.Int is based on time
Rand is typically seeded with time. ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Dan
![](http://wiki.compsci.ca/images/archive/3/3c/20100325043407!Danspic.gif)
|
Posted: Fri Nov 28, 2008 3:20 am Post subject: Re: RE:Rand.Int Not so random... |
|
|
Tony @ 28th November 2008, 12:15 am wrote: copthesaint @ Thu Nov 27, 2008 11:02 pm wrote: remember Rand.Int is based on time
Rand is typically seeded with time. ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif)
And it is noramly the time in seconds from the unix epoic and the seed is set when the program starts (also many langues like java also have a static counter in there random class so even random object created at the same time will have diffrent seeds), so no time the user starts the program should they get the same seed. The seed is used as input to the pesdo random algorthim to pick what set of pesdo random numbers to output witch should be unifromaly disbutried between the min and max number. If they are not messing with there system clock they should get diffrent numbers each time they start the program. Tho it is posible for some one to perdiect the next pesdo random number given enought numbers or the seed, witch is why it should not be used for security or anything that is exteramly imporant that it is random.
To my knowage there is no algorthim that can make turely random numbers with out any out side source and the best i have hured of are algorthims that take input from a seeminlgy random source, like noise/static on a radio or in a room or even an image of a laval lamp, and use that to creat randomish numbers. Tho as we get in to quantom computing it might become posible to have real random numbers.
If any one is intrested here are some random number services:
http://www.random.org/ - uses atmospheric noise
http://www.fourmilab.ch/hotbits/ - uses radioactive decay
There are also hardware generators that you can get for your computer that will help with true random number generation but turing is not going to work with them. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
![](images/spacer.gif) |
Vermette
![](http://compsci.ca/v3/uploads/user_avatars/637825944810b5d4444c6.jpg)
|
|
|
|
![](images/spacer.gif) |
DanielG
|
Posted: Fri Nov 28, 2008 6:41 pm Post subject: Re: RE:Rand.Int Not so random... |
|
|
insectoid @ Thu Nov 27, 2008 9:30 pm wrote: Daniel, this isn't 'bad' randomness, it is programming designed to fool you into thinking it is random, by clever (or perhaps mistaken) manipulation of variables.
I know that, all I was saying was that you could tell something is wrong with the code, since normally, no matter how bad the random numbers are, it wouldn't be that bad. |
|
|
|
|
![](images/spacer.gif) |
|
|