Jibrohni Posted April 17, 2010 Posted April 17, 2010 Hey all, I'm trying to generate a random array of 52 integers. My code below works, but doesn't seem to be very random. Now coming from VB I know there was a Randomize() function that helped with this, but I don't know how to achieve the same result in C#. Please help! private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(min, max); } public int[] GenerateDeck() { int[] DECK = new int[52]; //creates an array to hold thegenerated deck int[] holdDECK = new int[52]; int iArrayLength = 52; int iNextLocation = 0; //initialise tempDECK [1,2,3...52] for (int counter = 1; counter < 53; counter++) { holdDECK[counter - 1] = counter; } do { int rndNum = RandomNumber(0, iArrayLength); //generate random number between 1 and tDECK.Length // Find the next empty spot while (DECK[iNextLocation] != 0) { // Go until we find a zero (empty) iNextLocation++; } DECK[iNextLocation] = holdDECK[rndNum]; //shuffle down at this point do { holdDECK[rndNum] = holdDECK[rndNum + 1]; rndNum++; } while ((rndNum + 1) < iArrayLength); holdDECK[iArrayLength-1] = 0; iArrayLength--; } while (iArrayLength > 0); return DECK; Quote
Administrators PlausiblyDamp Posted April 17, 2010 Administrators Posted April 17, 2010 One thing I would do is move the line Random random = new Random(); out of the RandomNumber method as creating a new instance of the random number generator for each number is overkill. In fact given how the function is nothing more than a call to Random.Next() anyway I would probably remove the method entirely. In fact personally I would probably simplify the routine and use the built in Array.Sort and the random number generator combined to do all the heavy lifting here. Try the following and see if it does the job... public int[] GenerateDeck() { Random random = new Random(); int[] deck = new int[52]; for (int i = 0; i <= deck.Length-1; i++) deck[i] = i; byte[] randomData = new byte[deck.Length-1]; random.NextBytes(randomData); Array.Sort(randomData, deck); return deck; } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jibrohni Posted April 17, 2010 Author Posted April 17, 2010 Thank you very much, however with your method I noticed that the last number 51 is always in the last position at the end. Quote
Administrators PlausiblyDamp Posted April 17, 2010 Administrators Posted April 17, 2010 (edited) Oops - my bad, caught out by an "off by one error" again. Try byte[] randomData = new byte[deck.Length]; for the array declaration and it should work properly this time. Sorry ;) Edited April 17, 2010 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jibrohni Posted April 17, 2010 Author Posted April 17, 2010 Hmmm, that's what you had to begin with? Quote
Leaders snarfblam Posted April 17, 2010 Leaders Posted April 17, 2010 The first code listing was: byte[] randomData = new byte[deck.Length[b][color="Red"][i]-1[/i][/color][/b]]; The corrected code was byte[] randomData = new byte[deck.Length]; Confusing bounds with length is probably the most common cause of off-by-one errors, which is what it appears PD did. Quote [sIGPIC]e[/sIGPIC]
Administrators PlausiblyDamp Posted April 17, 2010 Administrators Posted April 17, 2010 I did, think I would be used to counting from 0 by now... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jibrohni Posted April 17, 2010 Author Posted April 17, 2010 Haha, I appreciate your help. C# is almost completely foreign to me at the mo. I haven't done it in ages. I got lazy and decided to use manly VB. MISTAKE. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.