Randomly Assigning Array

J

James

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
 
R

Rick Lones

James said:
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-
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Rick Lones said:
James said:
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-
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Rick Lones said:
James said:
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-
 
J

James

Thank you both...I was really looking for syntax but the thought processes
were helpful as well. Excellent.

Nicholas Paldino said:
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]
- (e-mail address removed)

Rick Lones said:
James said:
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-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top