General > Off Topic

Rnd function

<< < (2/9) > >>

Numsgil:
Sorry, I keep forgetting not everyone is a computer science and mathematics major :(

To answer your question, if VB works like C (and I have a hunch it does) this is how Randomize works:

Random numbers are generated from a function.  The next random number is generated from the one before it.  The next number should be statistically independant from the current number.

Here's a really bad but simple random function.  Let's pretend this is how VB does it:


--- Code: ---Function rand (seed as integer) as integer
rand = seed * 3 + 1

End Function
--- End code ---

Now, all random number generators have to be 'seeded'.  Since each value is made from the one before it, what do we do for the first value we want to generate?  We give it a 'seed value'.

Lets say we seed our generator with "1"

So we do this:


--- Code: ---dim seed as integer
seed = 1

while true
  seed = rand(seed)
  print.debug(seed)
Wend
--- End code ---

This shows us generating a list of 'random' numbers (I told you our function was bad but simple).

This will generate the same list of 'random' numbers everytime we run the program.   This is where psuedorandom space games like noctis and elite get their complexity.  They only have to store a single initial seed value and they can generate a list of random numbers to build their universe from.  It's the same for every time its run.

That's great, but what if we don't want the same list of random numbers everytime we run the program?  Well, since seed and rand(seed) are supposed to be statistically independant, as long as we can come up with a new seed everytime we run the program we can make a new, completely different random list.

What changes everytime the program is run?  That's right, the timer!  That blasted little bugger's been running down since sometime in the 1970s.  Always increasing, counting on and on, never tiring.

So we convert that timer value into whatever data format we're using, in my example an integer, and we use that as the seed each time the program's run.

That's what's happening when you type Randomize Timer.


Colleges will accept people that don't even know algebra.  Just means you get to pay them more money :rolleyes:

Botsareus:
Thanks Num. The next random number is generated from the one before it. Time to go write a new function for smexe.

Numsgil:
If you're making a new random number function (its kind of fun in a way) and you'd like to test if your new random number function is actually producing random numbers, you can try a simple stats correlation test.  You can do it in a Texas Instruments Ti83 plus (standard graphing calculator) by inputting all the numbers you get into a list and doing different regressions on it.  You should see a r^2 value of close to 0.  The closer to zero it is, the more random it is.

If you don't have a graphinc calculator, maybe you can find a similar program on the internet.  Or you can make your own!  Look up the algorithms for regression curves on any simple stats page on the internet.

Try exponential, linear, quadratic, logarithmic, and any others you can think of.

Botsareus:
ok  :ph43r: <--- thats supposed to be a ninja right?

Numsgil:
I'm not sure what emotion it's supposed to convey but it sure does look cool.   :ph43r:

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version