435,098 Members | 1,881 Online
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

# Randomly Assigning Array

 P: n/a Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks Nov 17 '05 #1
4 Replies

 P: n/a James wrote: Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. This isn't a C# question, but . . . 1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively. 2) Do a random shuffle of the array. Random shuffle: - Generate a random # n0 between 0 and 9. Swap array[n0] with array[9]. - Generate a random # n1 between 0 and 8. Swap array[n1] with array[8]. .. . . - Generate a random # n8 between 0 and 1. Swap array[n8] with array[1]. HTH, -rick- Nov 17 '05 #2

 P: n/a Rick, That won't work, because you can end up with more than one of any of the numbers (with the exception of 9) in the array elements. I believe the OP wanted to have the values 1-10 randomized in an array. This is how I would do it: // Generate the array. int[] arr = new int[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Cycle through the array, swapping out the current element with a random element. // Create the random number generator first. Random random = new Random(); // A temp value. int temp = 0; // The new index. int newIndex = 0; // Cycle through the elements. for (int index = 0; index < arr.Length; ++index) { // Swap element index out with another element. Get // the new index first. newIndex = random.Next(arr.Length); // Store the element. temp = arr[newIndex]; // Store the current element in the new index. arr[newIndex] = arr[index]; // Swap the element back. arr[index] = temp; } Hope this helps. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "Rick Lones" wrote in message news:_w******************@fe02.lga... James wrote: Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. This isn't a C# question, but . . . 1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively. 2) Do a random shuffle of the array. Random shuffle: - Generate a random # n0 between 0 and 9. Swap array[n0] with array[9]. - Generate a random # n1 between 0 and 8. Swap array[n1] with array[8]. . . . - Generate a random # n8 between 0 and 1. Swap array[n8] with array[1]. HTH, -rick- Nov 17 '05 #3

 P: n/a Rick, Oops! You were pretty much doing the same thing. I thought you were generating the value to go into each element as opposed to generating which element to swap with. My apologies. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "Rick Lones" wrote in message news:_w******************@fe02.lga... James wrote: Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. This isn't a C# question, but . . . 1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively. 2) Do a random shuffle of the array. Random shuffle: - Generate a random # n0 between 0 and 9. Swap array[n0] with array[9]. - Generate a random # n1 between 0 and 8. Swap array[n1] with array[8]. . . . - Generate a random # n8 between 0 and 1. Swap array[n8] with array[1]. HTH, -rick- Nov 17 '05 #4

 P: n/a Thank you both...I was really looking for syntax but the thought processes were helpful as well. Excellent. "Nicholas Paldino [.NET/C# MVP]" wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... Rick, Oops! You were pretty much doing the same thing. I thought you were generating the value to go into each element as opposed to generating which element to swap with. My apologies. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "Rick Lones" wrote in message news:_w******************@fe02.lga... James wrote: Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. This isn't a C# question, but . . . 1) Assign the numbers 1 - 10 to array slots 0 - 9, respectively. 2) Do a random shuffle of the array. Random shuffle: - Generate a random # n0 between 0 and 9. Swap array[n0] with array[9]. - Generate a random # n1 between 0 and 8. Swap array[n1] with array[8]. . . . - Generate a random # n8 between 0 and 1. Swap array[n8] with array[1]. HTH, -rick- Nov 17 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.