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 » Scripters
Home Search
 

Re: Make math.random more random?

Previous Thread :: Next Thread 
Evolution_Theory is not online. Evolution_Theory
Joined: 03 Feb 2012
Total Posts: 1770
26 Apr 2016 12:29 PM
math.randomseed(2,8)
for i = 1, math.random(2,8) do

it returns 2 95% of the time

if I use math.randomseed(tick()) then it's 2 all the time no matter what I do


The noobs shall be eradicated.
Report Abuse
xshot23 is not online. xshot23
Joined: 17 Oct 2014
Total Posts: 4873
26 Apr 2016 12:32 PM
when i do it

it doesnt give me 2 everytime

ur doing something wrong



http://www.roblox.com/xla-item?id=290739801http://www.roblox.com/aser-item?id=290739819http://www.roblox.com/23-item?id=290739831 R$79 Tx0 (づ ゚ ³ ゚)づ
Report Abuse
xLawOut is not online. xLawOut
Joined: 28 Feb 2015
Total Posts: 1782
26 Apr 2016 01:39 PM
first of all, it's not "math", it's match.


☮ MERCY
Report Abuse
Garfanzo is online. Garfanzo
Joined: 24 Apr 2015
Total Posts: 742
26 Apr 2016 01:56 PM
lastValue = 0
currentValue = 0

while wait(1) do

repeat wait() currentValue = math.random(2,8) until currentValue ~= lastValue
lastValue = currentValue

end

This script will give you more random numbers, and if you want it even more random then

lV1 = 0
lV2 = 0
lV3 = 0
lV4 = 0
currentValue = 0

while wait(1) do

repeat wait() currentValue = math.random(2,8) until
currentValue ~= lV1 and currentValue ~= lV2 and currentValue ~= lV3 and currentValue ~= lV4

lV4 = lV3
lV3 = lV2
lV2 = lV1
lV1 = currentValue

end

Maybe there is a more efficient way of doing this, but that works well though.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
26 Apr 2016 01:58 PM
I answered this question here:
https://forum.roblox.com/Forum/ShowPost.aspx?PostID=188028554
Report Abuse
ElectoStriking is not online. ElectoStriking
Joined: 10 May 2014
Total Posts: 1547
26 Apr 2016 02:05 PM
Are you related to Einstein?


Report Abuse
killerbot29003 is online. killerbot29003
Joined: 04 Oct 2014
Total Posts: 3054
26 Apr 2016 03:25 PM
math.randomseed(math.floor(os.time()))

try that
copied from loleris's tweet


Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
26 Apr 2016 04:24 PM
U could make it less time-based (The seed is pretty much set using os.time()) to more input based, which does sort of make it more randomized, as u never know what u'll press next, who knows maybe ur finger will slip and u'll accidentally press a key.

--Event
math.randomseed(math.random(tick()) * Key) --Key is basically the key code

This will then generate more random numbers rather than making math.random() depend on the last "random" number.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
26 Apr 2016 04:25 PM
The idea is to seed once. Seeding with every keypress results in less randomness.
Report Abuse
LongKillKreations is not online. LongKillKreations
Joined: 14 Feb 2014
Total Posts: 4502
26 Apr 2016 04:26 PM
math.randomseed(tick() % 1 * 1e6) works fine for me.

math.randomseed(tick() % 1 * 1e6)
for i = 1, 10 do
print(math.random(1, 10))
end

Output:
8
5
8
2
6
9
8
1
3


Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
26 Apr 2016 04:32 PM
Well yeah, but that's mainly cos ur always sort of resetting it, but if record the seed, then u can just add the Key code to it all the time, like so

local seed = math.random()
seed = seed ^ 2 --From what I remember that's how math libraries handle generating a new seed

--Event
math.randomseed(seed + Key)

Much better eh?
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
26 Apr 2016 04:35 PM
May I add, if u just add a relatively small number, there wont be much change, also, where's the randomability in generating a number that is already pretty much in store waiting for u to reveal it, if u can reset the chances for a new number with every key press, plus that most people wouldn't even know anything about that XD They'd be typing their messages and stuff, not realizing it's all used to generate a seed.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
26 Apr 2016 04:35 PM
Nope
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
26 Apr 2016 04:40 PM
Lol think whatever u want, but in my personal opinion, there's nothing more random than CONTINUOUS changes in the seed, cos like... the seed is ALREADY there, if the system knew what number range u were gonna give it next it could give away the numbers... but with the style of continuous changing the system wont know what hit it... and it also even said that in an article somewhere that changing the seed on key press is a good idea for randomobility :P U can't argue with that man. Btw, try the code and see it in action, cos what ur saying doesn't mean anything... I'd laugh if I tried my way and it would be random as hell :P
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
26 Apr 2016 05:00 PM
You clearly have zero clue what you are talking about.

I've looked at your way. The randomness is biased and limited.
From this page you can see that the largest keycode is ~1017, but there are only 254 actual numbers to use as seeds.
http://wiki.roblox.com/index.php?title=API:Enum/KeyCode


Let's take a look at the output for the first number your generator would spit out with each key. Note seed has zero impact on the first few numbers for each sequence, it has a visible effect on long sequences, but since you insist on resetting the sequence with every key press no sequence will run long enough to feel it.

local seed = math.random()
seed = seed^2
for i = 1, 1030 do
math.randomseed(seed+i)
print(math.random(100))
end

You also have to take the probability of each key being pressed into consideration. wasd and the number keys are the most likely to be pressed, so you're only really going to use the beginning of 14 different sequences a majority of the time. Now throw in the over lap that the beginning of the sequences have (as can be seen from the output) you're really not generating that many numbers, and the distribute is heavily biased.


This method is effective if you take multiple keypresses into account as well as other sources of entropy to generate a sequence of numbers, but you just switch between the the beginning of several predefined sequences which share a lot of overlap. Your method is terrible, end of story.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
26 Apr 2016 05:02 PM
**distribution is heavily biased


Unless you actually know how to write your own PRNG, the best option is to have a good way of choosing a random sequence generated by the lcg, this boils down to having a good way to choose a seed which I already provided.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
26 Apr 2016 05:24 PM
http://wiki.roblox.com/index.php?title=Random_numbers
Not sure if you read this article, but if you did. It does suggest using random events, but it suggests using them to operate on ONE sequence of random numbers. This to to say, you still want to have a good way of seeding the sequence, and then it uses events to increment through a sequence (effectively discarding random snippets of the sequence) that way when math.random() is called, you'll be pulling numbers from a semi-random place in the pseudo-random sequence generated by math.randomseed(seed). Doing it this way makes events additive in that the current place is the sequence is a function of how many times math.random() has been used by code and past random events.

Calling math.randomseed often with a poor seed source (key presses are a low entropy source) limits you to the beginning of only a few different sequences, so your distribution of random numbers is biased and random events are not additive as they just switch to a known place (the very beginning) of a different sequence.
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
27 Apr 2016 12:25 PM
Um... u didn't prove ur point... u didn't show me what u got... it would ONLY be considered if the results were similar to this
math.random(10)
>5
>5
>2
>1
>9
>7
>7
>7
(Just an example) this is what a bad result, if it's not ANYTHING like this, it IS more random that normal. Also, about the first few numbers, I didn't say u didn't have to call math.random() to "loosen it" up a bit.
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Apr 2016 01:43 PM
I think I made my point, you misquoted the article, have no idea how to properly seed (I showed that the seeding method you use leads to similar streams, and since your solution depends on frequently switching to the beginning of a few select streams-due to biased key usage-and only using the first few numbers this means your randomness is poor because it leads to output like you just posted), and you just admitted that your previously provided solution isn't necessarily complete.

"I didn't say u didn't have to call math.random() to "loosen it" up a bit"
Right, and I'm saying for what your doing even be remotely viable for all usage cases, this is a requirement. Without doing this your solution isn't random. And since your using keystrokes to poorly seed, you'll need another source of entropy to "loosen" those sequences to get some kind of randomness.

E.g. if someone needed to use math.random() once or twice a second, statistically they are only going to use the first two numbers of a select few sequences (i.e. wasd, spacebar, and number keys 1-5) since they variance in the first few numbers is small for all sequences seeded in the manner you described, math.random() will only provide a small handful of repeat numbers (amount 1-4). So in this case you don't have good randomness. This is my point, if you have a good way of generating numbers, it should have good randomness in all cases. From what you describe, it does not.


Better solution:

math.randonseed(tick()%1*1e6) --A good source for seeding, this alone is sufficient for most solutions
mouse.KeyDown:connect(function(key)
for i = 1, (tonumber(key:byte() or 0)) do --// discard segments of the sequence so we are choosing from a random place, entropy from keystrokes is cumulative
math.random()
end
end)
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
27 Apr 2016 02:15 PM
I guess roblox decided to remove the point of seeding because if you run this in the command bar:

for i = 1, 100 do
math.randomseed(100)
wait(1/20)
print(math.random(10000))
wait(1/17)
print(math.random(10000))
--// you should only be seeing two numbers repeated over and over since we reseed before calling math.random() twice
end

You'll notice there is variance with time; the point of randomseed should setting a sequence where the number you get is dependent on the number of times you've called math.random(), not when. If you cannot get 100% consistently repeatable results via seeding there really is no point in it.


So all in all, our argument is kinda moot. In theory, what you describe should be less random. In practice because roblox seems to salt math.random() based on when, not how often, it's called seeding doesn't matter as much
Report Abuse
mudkip99 is not online. mudkip99
Joined: 17 Jun 2008
Total Posts: 3362
27 Apr 2016 02:45 PM
Just use math.randomseed(os.time()), there's no real good reason to modify that any further. Unless you're doing some hardcore random number generation where numbers need to appear as random as physically possible, you should only need to call randomseed once per game instance (possibly once per script, I can't quite remember how it works in Roblox Lua) and seed it with the current time. If you need super pseudo-random numbers you should be writing your own PRNG and not using math.random() anyway (and if this prospect scares you and sounds like a lot of work, you probably don't need super hardcore random numbers).

Seed with os.time() or tick() once per game instance (once per script? again, can't quite remember) and you should never need to worry about it again. Modifying the value returned by os.time() or tick() any further is unnecessary, and in the case of modding it with 1e6 will only net one million possible seeds with the possibility of getting the same seed every once in a while (though very infrequently). os.time() will be far larger than that. I'm not sure how Lua compiles number values, but assuming it's a double, that means time will return a number between 0 and the max size of a double, which is way larger that 1e6 (1.7e308 is what I'm finding).

Truly (pseudo) random numbers will occasionally generate long strings of the same number, especially when they are within such a small range, that's just how they work. If you want more random seeming numbers you're going to have to use a wider range.
Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
27 Apr 2016 05:36 PM
Well yeah, that's a pretty good seeding technique, though I will try mine out, if it is like u stated, I'll find a way out (As I always do) so tomorrow is the day when the truth comes out XD And it not only has to be keydown, it can also be for example... how many children are in a workspace (listen to that with events), walking patterns (I'm pretty sure that in adventure games, or FPS games people will be all over the place trying to stay alive or to explore NEW regions), etc.
Report Abuse
claire7210 is not online. claire7210
Joined: 02 Feb 2014
Total Posts: 41
27 Apr 2016 05:37 PM


What's the time?
Well it's gotta be close to midnight
My body's talking to me
It says, Time for danger

It says, I wanna commit a crime
Wanna be the cause of a fight
I wanna put on a tight skirt
And flirt with a stranger

I've had a knack from way back
At breaking the rules once I learn the games
Get up! Life's too quick, I know someplace sick
Where this chick'll dance in the flames

We don't need any money
I always get in for free
You can get in too
If you get in with me

Let's go out tonight
I have to go out tonight
You wanna play? Let's run away
We won't be back before it's New Year's Day
Take me out tonight, meow

When I get a wink from the doorman
Do you know how lucky you'll be?
That you're on line with the feline
Of Avenue B

Let's go out tonight
I have to go out tonight
You wanna prowl, be my night owl?
Well take my hand we're gonna howl
Out tonight

In the evening I've got to roam
Can't sleep in the city of neon and chrome
Feels too damn much like home
When the Spanish babies cry

So let's find a bar
So dark we forget who we are
Where all the scars
From the nevers and maybes die

Let's go out tonight
I have to go out tonight
You're sweet, wanna hit the street?
Wanna wail at the moon like a cat in heat?
Just take me out tonight

Please take me out tonight
Don't forsake me, out tonight
I'll let you make me out tonight
Tonight, tonight, tonight

Read more: RENT - Out Tonight Lyrics | MetroLyrics
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
28 Apr 2016 12:39 AM
"Seed with os.time() or tick() once per game instance (once per script? again, can't quite remember) and you should never need to worry about it again. Modifying the value returned by os.time() or tick() any further is unnecessary, and in the case of modding it with 1e6 will only net one million possible seeds with the possibility of getting the same seed every once in a while (though very infrequently). os.time() will be far larger than that. I'm not sure how Lua compiles number values, but assuming it's a double, that means time will return a number between 0 and the max size of a double, which is way larger that 1e6 (1.7e308 is what I'm finding)."

You're discounting the fact that with respect to time a large portion of tick() or os.time() remains constant and the Most Significant Bits bias the LCG into returning the same numbers if math.randomseed(os.time()) or math.randomseed(tick()) is used often in a small period of time, you get little change.

This leads people to thinking the math.random() isn't random because when they test things in playsolo the seeding method gives you the same result (hence this thread, hence the thread I linked to, etc) because delta_time is too small. Hence I suggest seeding with math.randomseed(tick()%1*1e6). Assuming that math.randomseed(tick()) results in a different number sequence everytime it increments by one second it'll take almost a year to see//use one million different streams of numbers. My suggestion gives you access to 1 million different streams instantly


So sure, os.time() gives you the full seeding power of a double, but you'll have to wait an extremely long time to get any utility out of it. In the short term, it's a terrible source for seeding. I agree seeding once is all that is required unless you know what you're doing, but sometimes someone might require a robust seeding method. What I've suggested works with sub millisecond step times between seeding, unlike os.time() or tick() which need at least a one second step time for noticeable variance.

for i = 1, 100 do
math.randomseed(tick()%1*1e6)
print(math.random(), math.random(), math.random())
end
print()
for i = 1, 100 do
math.randomseed(tick())
print(math.random(), math.random(), math.random())
end
print()
for i = 1, 100 do
math.randomseed(os.time())
print(math.random(), math.random(), math.random())
end
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
28 Apr 2016 12:58 AM
The misinformation is real.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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