Created
May 2, 2013 17:43
-
-
Save jakeobrien/5503948 to your computer and use it in GitHub Desktop.
Shuffle logic for Domino! bones
This file contains 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
// for native iOS (Objective-C) | |
srand((unsigned)time(NULL)); | |
NSUInteger count = [bones count]; | |
for (NSUInteger i = 0; i < count; ++i) { | |
int nElements = count - i; | |
int n = (rand() % nElements) + i; | |
[bones exchangeObjectAtIndex:i withObjectAtIndex:n]; | |
} | |
// for Unity (C#) | |
public static void Shuffle<T>(this List<T> list) | |
{ | |
System.Random rnd = new System.Random(); | |
int n = list.Count; | |
while (n > 1) { | |
n--; | |
int k = rnd.Next(n + 1); | |
T obj = list[k]; | |
list[k] = list[n]; | |
list[n] = obj; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! Dave here. This is the actual code that we are using to randomize bones. Up top is what we are currently using for iOS. Below that - "For Unity (C#)" - is the new code that we will be using in our Android and future iOS app.
If you have any questions or comments about this, let us know!