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!
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!
Code:
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;