|
| 05 Jan 2014 09:52 PM |
| Dunno why I've been having so much trouble with this. I need a random number generator that doesn't repeat a number it has already outputted. Can anyone hook me up? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 05 Jan 2014 09:54 PM |
Then it's not even random at all.
local used = {}
local function pickRandom(s,e) local num repeat num = math.random(s, e) until not used[num] used[num] = true return num end |
|
|
| Report Abuse |
|
|
redlo123
|
  |
| Joined: 07 Feb 2013 |
| Total Posts: 231 |
|
|
| 05 Jan 2014 09:55 PM |
seed it first
next, every time it generates a number, add it to an array. Check if the number has already been generated by looking through the array with loop |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 05 Jan 2014 10:00 PM |
| In fact, a non-repeating random number generator is less random if that's what you're aiming for. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2014 10:09 PM |
The reason I needed it (thanks, cntkillme!) was to be able to put items into a table in a random order. If I tried using truly random numbers to iterate over the items, some of them would be picked more than once.
I've been attempting to use the same strategy as redlo and cntkillme for a couple of days now, but it wasn't working for me :/ |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2014 10:42 PM |
| For that, use a numeric for loop to go through the table, selecting a random element from the table and inserting it into the second table then removing the selected element. |
|
|
| Report Abuse |
|
|
|
| 05 Jan 2014 11:33 PM |
Will this work for string.gmatch? I guess I'd might as well explain further.
I am trying to scramble a word by exploding it into individual letters, then picking them at random and putting them in another table. Then, I'll combine the scrambled letters again by using table.concat, though I'm getting weird errors there for some reason. |
|
|
| Report Abuse |
|
|