2e9
|
  |
| Joined: 16 Nov 2013 |
| Total Posts: 74 |
|
|
| 17 Apr 2016 10:25 PM |
| i tried alot of different ways, and scripts, and none work. Can someone please help me out? i just need it to not pick the same number again. |
|
|
| Report Abuse |
|
|
nox7
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 27467 |
|
|
| 17 Apr 2016 10:31 PM |
I think the best way is to make your own function that uses math.random(), but also checks a table and repeats it if the number is already chosen. Like this:
local function mathRandom(min, max, alreadyChosenList) local chosen local alreadyChosen alreadyChosenList = alreadyChosenList or {} local function isInList(tab, val) local inList = false for i,v in pairs(tab) do if (v == val) then inList = true break end end return inList end repeat chosen = math.random(min, max) until (not isInList(chosen, alreadyChosenList) table.insert(alreadyChosenList, chosen) return chosen, alreadyChosenList end
local list for i = 1, 5 do local num num,list = mathRandom(1,5) print(num) end
-----
This will never pick the same number twice. |
|
|
| Report Abuse |
|
|
LucasLua
|
  |
| Joined: 18 Jun 2008 |
| Total Posts: 7386 |
|
|
| 17 Apr 2016 10:54 PM |
| That code will also only pick 5 numbers. Then the sixth time you call it, the code will drop into an infinite loop. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 17 Apr 2016 11:02 PM |
Lol. Literally everything nox had in under 20 lines.
local nums = {}
prevNum = function(x) for i,v in next, nums do if x == v then return true end end end
while wait(1) do local x = math.random(1,10) while prevNum(x) do x = math.random(1,10) wait() end table.insert(nums,x) print(x) end
|
|
|
| Report Abuse |
|
|
| |
|
|
| 18 Apr 2016 07:14 AM |
"what awful solutions."
So provide better ones? Don't prove just how useless you are so easily. |
|
|
| Report Abuse |
|
|