Author Topic: Random Number question  (Read 8431 times)

0 Members and 1 Guest are viewing this topic.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Random Number question
« 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.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random Number question
« Reply #1 on: March 17, 2011, 01:49:17 am »
* Qwerty.55 <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.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Random Number question
« Reply #2 on: March 17, 2011, 01:53:49 am »
Okay, I'll give that a try :) Thanks!

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random Number question
« Reply #3 on: March 17, 2011, 01:55:27 am »
Actually, those instructions might be kind of confusing, so here:



Of course, if you want to give me some sample numbers... ;)
« Last Edit: March 17, 2011, 02:01:59 am by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Random Number question
« Reply #4 on: March 17, 2011, 02:03:59 am »
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)
« Last Edit: March 17, 2011, 02:05:20 am by leafiness0 »
In-progress: Graviter (...)

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random Number question
« Reply #5 on: March 17, 2011, 02:10:50 am »
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 offers a very good suite of tools to test for randomness.
« Last Edit: March 17, 2011, 02:39:10 am by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Random Number question
« Reply #6 on: March 17, 2011, 03:01:03 am »
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?

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random Number question
« Reply #7 on: March 17, 2011, 03:16:43 am »
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?

« Last Edit: March 17, 2011, 03:17:44 am by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Random Number question
« Reply #8 on: March 17, 2011, 03:28:00 am »
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)

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random Number question
« Reply #9 on: March 17, 2011, 03:53:19 am »
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.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Random Number question
« Reply #10 on: March 17, 2011, 03:59:14 am »
Okay, so the way I come up with the original rand is by doing this:
Code: [Select]
ld b,2
Loop:
 ld a,r
 ld d,a
 ld a,r
 ld e,a
 add hl,de
 djnz Loop

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random Number question
« Reply #11 on: March 17, 2011, 04:05:52 am »
Ah, so that's essentially a variation on an LCRNG. Yeah, that should work if the value of N is constant through the evaluation.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Random Number question
« Reply #12 on: March 17, 2011, 04:10:10 am »
What is LCRNG? I can guess the RNG...

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Random Number question
« Reply #13 on: March 17, 2011, 04:14:17 am »
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.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Random Number question
« Reply #14 on: March 17, 2011, 04:17:02 am »
The N2 provides an upper bound for the random number because it is (mod N2).