This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Jappards's profile picture

Random Number API (more random than math.random()!)

Started by Jappards, 08 July 2013 - 09:55 AM
Jappards #1
Posted 08 July 2013 - 11:55 AM
when i looked at the math library tutorial, i was shocked of the effectiveness of math.random(), because math.random() is pseudo random, not truly random, ok, this API isn`t truly random either, but it`s very close to it,so i began to improve the PRNG (pseudo-random number generator) and this is the result.

features:
Spoiler

RandomNumber.genNumber()--doesn`t accept an argument, generates a random number
RandomNumber.genBetween(min,max)--generates a random number, min is the minimal amount it needs to be, guess what`s max for? this returns 2 parameters: randomnumber, remainder (remainder of my algorithm)

code:
http://pastebin.com/nZAfgJVk

usage:
1.pastebin get nZAfgJVk RandomNumber
2.os.loadAPI("RandomNumber")
3.????
4.PROFIT!!!
Lymia Aluysia #2
Posted 08 July 2013 - 02:03 PM
http://pastebin.com/8YKL5g7j


This is an… interesting, ah, "concept"…
Oddstr13 #3
Posted 08 July 2013 - 02:09 PM
I am sorry that i wasn't able to get this thru to you on IRC.

The number returned by math.random() isn't 0 or 1, it is a decimal number between 0.0 and 1.0 with several decimal places.
Let's try multiplying that number with 10^7.

We now suddenly have a number between 0 and 10000000, that is, zero and ten millions.
And we might still have a few decimals left.

Besides that fact, this is what happened when i tested your code;
your api is saved as rnd, then loaded with os.loadAPI("rnd")


As you can see, genNumber() is far from random, and genBetween() dosn't exactly return what i would expect.



Best Regards
Oddstr13
Mikeemoo #4
Posted 08 July 2013 - 02:26 PM
lol
Lyqyd #5
Posted 08 July 2013 - 03:00 PM
This is both useless and misleading. It is also less random than unseeded math.random.

Just use math.random.