Hey all, I'm just starting out using C# and am trying to learn by converting a program I've started in VB to C#.
Sadly I'm stuck early on with trying to generate an array of 52 randomly ordered integers. I have it working but fear that I'm not doing a tidy job. The routine seems very sluggish as opposed to it running in VB. Please can anyone advise on a better way to do this than my method below please?
I'm using a goto statement which I have a bad feeling about and it's also doing a fair few iterations that can most likely be avoided. Even so, it shouldn't be taking my nice new i5 processor 3-4 seconds??
I really appreciate your time. P.S. I like the efficiency and neatness of C# coding. Shame I'm crap at it...
Sadly I'm stuck early on with trying to generate an array of 52 randomly ordered integers. I have it working but fear that I'm not doing a tidy job. The routine seems very sluggish as opposed to it running in VB. Please can anyone advise on a better way to do this than my method below please?
I'm using a goto statement which I have a bad feeling about and it's also doing a fair few iterations that can most likely be avoided. Even so, it shouldn't be taking my nice new i5 processor 3-4 seconds??
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 iCounter = 1;
do
{
Start:
int rndNum = RandomNumber(0, 52);
if (DECK[rndNum] == 0)
{
DECK[rndNum] = iCounter;
iCounter++;
}
else
goto Start;
} while (iCounter < 53);
return DECK;
}
private void btnGenerate_Click(object sender, EventArgs e)
{
GenerateDeck();
}