Controlled Posted July 9, 2014 Share Posted July 9, 2014 Was wondering how to get a different math.random result if the number is the same as the last time. So in this script position is saved, and it checks if it was the old position. But I don't know how to get another math.random thats not the same.... heres the script pos = math.random(1,#cams); math.random(1,#cams); math.random(1,#cams) local x, y, z = cams[pos][1], cams[pos][2], cams[pos][3] if pos1 then if pos==pos1 then -- Something end end --pos = math.random(1, #cams) local tx, ty = cams[pos][4], cams[pos][5] local cx, cy, cz = cams[pos][6], cams[pos][7], cams[pos][8] pos1 = pos Link to comment
xXMADEXx Posted July 9, 2014 Share Posted July 9, 2014 Define a variable to the old pos, and use a while loop. Inside the loop just keep generating a new pos until it's not the same as the old one. Link to comment
Controlled Posted July 9, 2014 Author Share Posted July 9, 2014 Define a variable to the old pos, and use a while loop. Inside the loop just keep generating a new pos until it's not the same as the old one. Working! Thanks Link to comment
vx89 Posted July 9, 2014 Share Posted July 9, 2014 OFFTOPIC: Random that isn't random reminded me this article: http://labs.spotify.com/2014/02/28/how- ... fle-songs/ Link to comment
tosfera Posted July 9, 2014 Share Posted July 9, 2014 OFFTOPIC: Random that isn't random reminded me this article: http://labs.spotify.com/2014/02/28/how- ... fle-songs/ Random is never random because a computer will randomly pick a new number without keeping his old number so the chance is big to get the same number again ( try to random between 0 and 2. 80% of the time you'll get 1. ). But this only occurs with low numbers, if you take a math.random between 0 and 1000, the chance is VERY low to get a duplicate. Link to comment
vx89 Posted July 10, 2014 Share Posted July 10, 2014 Random is never random because a computer will randomly pick a new number without keeping his old number so the chance is big to get the same number again ( try to random between 0 and 2. 80% of the time you'll get 1. ). But this only occurs with low numbers, if you take a math.random between 0 and 1000, the chance is VERY low to get a duplicate. Theoretically, the chance to get any number is same every time. I'm not sure what did you base your example on, but here's a quick javascript oneliner to prove it wrong (just run it in your browser console). var numbers = []; numbers[0] = 0; numbers[1] = 0; numbers[2] = 0; for (i = 0; i < 1000; i++) { var r = Math.floor(Math.random() * 3); numbers[r]++; } console.log("0:"+numbers[0] + ", 1:"+numbers[1] +", 2:"+numbers[2]) Link to comment
tosfera Posted July 10, 2014 Share Posted July 10, 2014 Random is never random because a computer will randomly pick a new number without keeping his old number so the chance is big to get the same number again ( try to random between 0 and 2. 80% of the time you'll get 1. ). But this only occurs with low numbers, if you take a math.random between 0 and 1000, the chance is VERY low to get a duplicate. Theoretically, the chance to get any number is same every time. I'm not sure what did you base your example on, but here's a quick javascript oneliner to prove it wrong (just run it in your browser console). var numbers = []; numbers[0] = 0; numbers[1] = 0; numbers[2] = 0; for (i = 0; i < 1000; i++) { var r = Math.floor(Math.random() * 3); numbers[r]++; } console.log("0:"+numbers[0] + ", 1:"+numbers[1] +", 2:"+numbers[2]) I based my examples on my experience of programming/scripting and developing of the past 9 years. You might get the same number with a random from 0 to 1000 but since a lot of languages don't floor the number, it's very rare to get the same numbers. I see you used the 'floor' in your javascript, which would change the entire conclusion of the random. If you take a look at php and give it a math.random from 0 to 1000, gl getting the same one. Just do a loop from 0 to 1000 and see that there is 1 or 2 of the same values in there. Has to do something with the way how binary works or something like that haha. edit; taking my words back. Somehow the random is changed in the new version of PHP and OS or something like that. Did a new test with 1000 and got quite alot of duplicates. Well, that sucks. Easy way to solve this is just store your randoms in an array and check if the random is there. If so, create a new random until you do have a random one. Link to comment
vx89 Posted July 10, 2014 Share Posted July 10, 2014 There's some confusion here. I'm saying that the chance of getting some specific value more than other values is not high. You said that there is 80% chance to get 1 out of 0,1 and 2: try to random between 0 and 2. 80% of the time you'll get 1. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now