nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 06:48 PM |
I thought I'd have a go and try to make the most random pseudo number generator that I could and came up with something that generates, what seems to be, fairly random numbers.
I've ran a series of tests, checking to see what the average of the random numbers generated would be and usually comes at from 4.6 - 5.4, but can realistically range from 1 - 10. This is the same case for Lua 5.2s math.random function, so mine seems to pass that test.
Another thing I did was see averagely what numbers would generate (for instance, I might generate a list of numbers and see if a certain digit is biased) and it seemed to average out well, as well.
But, because I can't predict the results, I can only average it out. So technically some results might be exactly what I was looking for; however, I might run the same test and the results might be completely different.
Anyway, here's the code, along with some results:
-- (min, max [, accuracy [, seed]]) function random(min, max, accuracy, seed) accuracy = (accuracy and accuracy > 100 and 100) or 100 seed = seed or (os and os.time() or tick()) function randomizeSeed(seed) -- Make our seed more "random" so a number only repeats more than once pseudo randomly local sum = 0 for n = 1, 100, 101 - accuracy do local num = (seed * n) % tonumber(string.reverse(n)) -- Something I just went with sum = sum + num end return seed % sum end local reseed = randomizeSeed(seed) local rnum = reseed % (max + 1 - min) + min return math.floor(rnum) end
for i = 1, 10 do print(random(1000, 2000, 100, tick() * i)) end
> 1372 > 1224 > 1194 > 1345 > 1914 > 1137 > 1353 > 1069 > 1532 > 1979
print(random(123, 789)) --> 459 print(math.random(123, 789)) --> 551
Please point out any flaws you see. |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 06:49 PM |
"try to make the most random pseudo number generator that I could" meant to say instead, "try to make the most pseudo random number generator that I could" |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 06:57 PM |
| Also note that the signifigance of this is that it works in real Lua, (as it works in roblox lua too, but the RandomsizeSeed function isn't necessary.) |
|
|
| Report Abuse |
|
|
| |
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 07:06 PM |
"(as it works in roblox lua too, but the RandomsizeSeed function isn't necessary.)"
said that after forgetting about an issue,
for i = 1, 10 do print(random(1, 10, 100, tick() * i)) end
will yield results such as this (occasionally, while some results might go well), without the randomizeSeed funtion:
> 10 > 9 > 8 > 7 > 6 > 5 > 4 > 4 > 3 > 2
(actual results)
this is a problem with tick() or os.time() being a unsuitable number... but I forgot the mathematical reason to this so I won't go into much more details. |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:07 PM |
"as it works in roblox lua too"
No? The os library is disabled in ROBLOX Lua. The entire seed portion of this would break.
http://wiki.roblox.com/index.php/User:ElectricBlaze |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:07 PM |
| Pretty interesting. I like how it randomizes the seed as well |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:09 PM |
@ElectricBlaze
Look at his code:
seed = seed or (os and os.time() or tick())
It checks to see if 'os' exists or not before using it, else it just uses tick, assuming it's in Roblox. |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 07:10 PM |
| No. Also, "while some results might go well" don't know why I said that, as the results are always odd, and often patterns (like 1, 6, 1, 6, ... but like I said, I don't know the complete math behind this.) |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:12 PM |
Oh, indeed, sorry. I just skimmed it over; I didn't notice that. :c
http://wiki.roblox.com/index.php/User:ElectricBlaze |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 07:13 PM |
"Pretty interesting. I like how it randomizes the seed as well"
Seemed like the only thing I could do, as the results I was getting with the plain seed were getting results spoken about in my previous posts.
Most of the (not too much) time put towards this was testing to see if it's actually random. |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:15 PM |
If it interests you at all, you can find part of the actual source code for Lua's math.random function here:
http://www.lua.org/source/5.1/lmathlib.c.html |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:15 PM |
I wrote a paper explaining random number generation for extra credit in my Calc class. Would you like to read it? |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:19 PM |
You could probably add a little part to it so when you do
random(100)
It detects 100 as Max. Maybe something like
if (min) and (max == nil) then min = max end |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 07:19 PM |
"If it interests you at all, you can find part of the actual source code for Lua's math.random function here:
http://www.lua.org/source/5.1/lmathlib.c.html"
Cool page there, tried to find a similar function but couldn't find anything more than vague formulas. Interesting on the relations between my attempt and the actual function. |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 27 Sep 2012 07:19 PM |
"I wrote a paper explaining random number generation for extra credit in my Calc class. Would you like to read it?"
I would love to. |
|
|
| Report Abuse |
|
|
|
| 27 Sep 2012 07:25 PM |
I can't seem to find it... :( |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
| |
|
|
| 27 Sep 2012 07:30 PM |
I'm going home next week. I'll try to get into my high school to find my files. It should be on my school account since it doesn't seem to be on my computer.
Until then, I'll track this thread.
:) |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
| |
|
iOpened
|
  |
| Joined: 31 Mar 2012 |
| Total Posts: 638 |
|
| |
|
|
| 08 Oct 2012 10:48 PM |
https://docs.google.com/open?id=0B4UG4d40O4VHWDY2Tkd2Y1BaT28
My extra credit report. It's got a bit of fluff in it with respect to content; it doesn't really explain why it works, just how it works, and I have a lot of page fillers that don't really have anything to do with random numbers. ;P
Nonetheless I think you will like the read. |
|
|
| Report Abuse |
|
|
|
| 08 Oct 2012 10:51 PM |
@AFF - Did the forum filter really not catch that?
http://wiki.roblox.com/index.php/User:ElectricBlaze |
|
|
| Report Abuse |
|
|
|
| 08 Oct 2012 10:53 PM |
y u so using bullet points in formal writing? :P
http://wiki.roblox.com/index.php/User:ElectricBlaze |
|
|
| Report Abuse |
|
|
|
| 08 Oct 2012 10:57 PM |
And I'll bet your teacher LOVED that you used wikipedia as your only source. :P
http://wiki.roblox.com/index.php/User:ElectricBlaze |
|
|
| Report Abuse |
|
|