Omnimaga
General Discussion => Other Discussions => Math and Science => Topic started by: Xeda112358 on March 17, 2011, 01:30:28 am
-
Okay, so I made a routine in ReCode to generate a random integer and I wanted to test how random it is. Does anybody know of a way to do this? I am working on a mini game that uses a 4x4 grid and it initializes the game by selecting a random plot on the grid. I then got the idea to test how long it would take for it to randomly select each plot and then I wanted to test the "randomness" of certain routines. So that brings me to my question...
Anyway, here is a screenshot showing what I was talking about.
-
/me <3 random numbers
The most important question is how do you generate the numbers? That will likely suggest more appropriate and conclusive tests.
Now, without that knowledge, you can do several quick and easy tests just to determine general randomness. Since Recode can presumably operate on OS variables, I'll assume that you'll do that rather than write the tests themselves.
The simplest test:
1) Generate a LOT of numbers (500-900 or so) and store them in a list (L2).
2) Fill another list with the value of the index of the list up to the dimensions of the first list. For example, L1(1)=1, L1(2)=2, L1(3)=3,...,etc
3) Conduct a linear regression on the list. This would be LinReg(ax+b) L1,L2,Y1 where L1 is your index list. Make sure Diagnostics are on. r2 should generally have a very low value (>0.01)
3) Then, do L2-Y1(L1) in list L3.
4) Perform the linear regression LinReg(ax+b) L1,L3,Y1 with diagnostics on again. A, B, r2, and r should all be extremely close to 0. If they are not, then you have a problem.
5) Repeat a few times. The trouble with randomness is that it sometimes doesn't appear random.
-
Okay, I'll give that a try :) Thanks!
-
Actually, those instructions might be kind of confusing, so here:
(http://img.removedfromgame.com/imgs/Random.gif)
Of course, if you want to give me some sample numbers... ;)
-
Theres always some degree of bias. I think one of the better methods I saw was to like measure the electrical modulation at the io port or something crazy like that, then generate a random number based on the square root of seven.
The best way to check is probably linreg or a similar regression function, then check the r and r squared values (closer to one is bad)
-
Xeda would be uber-1337 if she could use electrical currents to generate random numbers using the TI-84+ SE's z80 :P
It's too bad that calculators don't have hard drives, either. The access times on those make great random number generators.
EDIT: Xeda, if you don't mind the extra effort of using a hex editor, the windows software available here (http://stat.fsu.edu/pub/diehard/) offers a very good suite of tools to test for randomness.
-
Okay, so the first round I get an r value of around .02, then when I work with L3, I get an r value of about .98
I take it that isn't very random?
-
O.O
I wish half of my normal data got correlations that high...
.98 means that the linear regression can account for about 98% of the data. Did you try the test multiple times?
-
I just modified the code a little... Currently rand returns a "random" integer from 0 to 65535, but before was doing (rand*(rand mod(5))) (mod 128) because that was much more random However, I just changed it to this:
(rand*(rand (mod n2))) (mod n)
And yes, I did do several trials :D Now, here are 12 trials, 6 with using L1 and L2:
.11
-.13
.11
.02
.009
.01
And 6 with L3,L2, and L1:
-.09
.02
-.01
.003
.14
-.03
(those are all r values)
-
Those are definitely acceptable for most purposes, but they're not quite random. How do you generate the initial Rand?
PS: The r2 value is the important one. The r value doesn't really have much meaning in this context.
-
Okay, so the way I come up with the original rand is by doing this:
ld b,2
Loop:
ld a,r
ld d,a
ld a,r
ld e,a
add hl,de
djnz Loop
-
Ah, so that's essentially a variation on an LCRNG. Yeah, that should work if the value of N is constant through the evaluation.
-
What is LCRNG? I can guess the RNG...
-
Linear Congruential Random Number Generator. It typically refers to things generated using a specific formula, but I can see that if the N2 value is constant, your algorithm with produce similar results, probably with a roughly equivalent period too.
-
The N2 provides an upper bound for the random number because it is (mod N2).
-
Well, actually N provides the upper bound for the random number because everything is (mod N) ;)
-
Yeah, sorry :D Actually, I just realised I typed up the wrong equation...
What I meant to say is that I multiply rand by floor(rand/N2) and then take mod N of that result.
I don't know why I said N2 provided an upper bound ...
-
I think you should change it to be n^2. That way your users can scratch their heads trying to figure out how to get Rand mod 2 with integers...