Stevor Posted March 13, 2021 Share Posted March 13, 2021 hello i have script with use math.random ( First Number , second Number ) but some times the number same old number ( spam ) what can do Link to comment
Tekken Posted March 13, 2021 Share Posted March 13, 2021 You mean it outputs two times the same number in a row? If so you can store the old number in a variable and check if is the same or not and store the number in var again. Something like this: local lastnr = math.random(0, 9); function generatenr() local newnr = math.random(0, 9); if newnr ~= lastnr then print(newnr); lastnr = newnr; --overwrite old nr else generatenr();--recall function if the same nr end end Link to comment
Stevor Posted March 14, 2021 Author Share Posted March 14, 2021 13 hours ago, Tekken said: You mean it outputs two times the same number in a row? If so you can store the old number in a variable and check if is the same or not and store the number in var again. Something like this: local lastnr = math.random(0, 9); function generatenr() local newnr = math.random(0, 9); if newnr ~= lastnr then print(newnr); lastnr = newnr; --overwrite old nr else generatenr();--recall function if the same nr end end when i use lastnr to orginal random outputChatBox(lastnr,0,0,0,true) --after start or restart its same number export --and --when i use newnr outputChatBox(newnr,0,0,0,true) --debug bad argument Link to comment
Tekken Posted March 14, 2021 Share Posted March 14, 2021 at the top of your script add this math.randomseed(getTickCount()); This will make it so the resource won't output the same random numbers every time it's restarted! Link to comment
Stevor Posted March 14, 2021 Author Share Posted March 14, 2021 27 minutes ago, Tekken said: at the top of your script add this math.randomseed(getTickCount()); This will make it so the resource won't output the same random numbers every time it's restarted! but what to use ? newnr or lastnr Link to comment
Tekken Posted March 14, 2021 Share Posted March 14, 2021 You use newnr if it's not the same as the lastnr if it is the same you just redo math.random to have a different one Link to comment
Stevor Posted March 14, 2021 Author Share Posted March 14, 2021 4 minutes ago, Tekken said: You use newnr if it's not the same as the lastnr if it is the same you just redo math.random to have a different one same thing bad argument when i use it i need to use 1 math.random only i can't use 2 Link to comment
SpecT Posted March 14, 2021 Share Posted March 14, 2021 This should work for you: local lastNumber function getRandomNumber(from, to) local randNumber = math.random(from, to) -- generate random numbers until randNumber is different than lastNumber while(randNumber == lastNumber) do randNumber = math.random(from, to) end lastNumber = randNumber return randNumber end function testerino() outputChatBox(getRandomNumber(1,5)) end addCommandHandler("ok",testerino) Link to comment
Stevor Posted March 14, 2021 Author Share Posted March 14, 2021 2 hours ago, SpecT said: This should work for you: local lastNumber function getRandomNumber(from, to) local randNumber = math.random(from, to) -- generate random numbers until randNumber is different than lastNumber while(randNumber == lastNumber) do randNumber = math.random(from, to) end lastNumber = randNumber return randNumber end function testerino() outputChatBox(getRandomNumber(1,5)) end addCommandHandler("ok",testerino) thx it's work good 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