Last active
March 20, 2016 10:29
-
-
Save balaam/f1c1ad8b0a69c00adccd to your computer and use it in GitHub Desktop.
Quick code sketch for picking from six cards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function shuffle(t) | |
| local n = #t | |
| while n > 2 do | |
| local k = math.random(n) | |
| t[n], t[k] = t[k], t[n] | |
| n = n - 1 | |
| end | |
| return t | |
| end | |
| local t = | |
| { | |
| index =1, | |
| last_pick = -1, | |
| picks = {1,2,3,4,5,6}, | |
| pick = function(self) | |
| if self.index > #self.picks then | |
| self.picks = shuffle(self.picks) | |
| self.index = 1 | |
| if self.picks[self.index] == self.last_pick then | |
| self.picks[self.index], self.picks[#self.picks] = self.picks[#self.picks], self.picks[self.index] | |
| end | |
| end | |
| local pick = self.picks[self.index] | |
| self.last_pick = pick | |
| self.index = self.index + 1 | |
| return pick | |
| end | |
| } | |
| math.randomseed(os.time()) | |
| math.random() | |
| shuffle(t.picks) | |
| for i = 1, 25 do | |
| print(t:pick()) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment