generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Pseudo Random Number Generator

Previous Thread :: Next Thread 
nate890 is not online. 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 is not online. 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 is not online. 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
xXTheRobotXx is not online. xXTheRobotXx
Joined: 05 May 2012
Total Posts: 13343
27 Sep 2012 06:57 PM
you just blew my mind

Report Abuse
nate890 is not online. 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
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
27 Sep 2012 07:07 PM
Pretty interesting. I like how it randomizes the seed as well
Report Abuse
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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 is not online. 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
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
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 is not online. 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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
XxChopSlaperxX is not online. XxChopSlaperxX
Joined: 09 Nov 2010
Total Posts: 3112
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 is not online. 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 is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
27 Sep 2012 07:25 PM
I can't seem to find it...
:(
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
27 Sep 2012 07:26 PM
d'aaaawwww
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
28 Sep 2012 06:29 AM
Sounds like a plan =D
Report Abuse
iOpened is not online. iOpened
Joined: 31 Mar 2012
Total Posts: 638
07 Oct 2012 07:14 PM
Thankyou.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
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
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
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
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image