0

The quest is solve the problem only with just these things that I (we) have learned. My program is working except i get duplicated random numbers. Please help how to do it, i'm trying since a half day:(

int[] lotto = new int[6];
Random rnd = new Random();
int i;
int max, min;       

for (i = 0; i < 6; i++)
{
    lotto[i] = rnd.Next(1, 46);
    Console.Write("{0} ", lotto[i]);
}

max = lotto[0];
min = lotto[0];

for (i = 0; i < 6; i++)
{
    if (lotto[i] > max) max = lotto[i];
    if (lotto[i] < min) min = lotto[i];
}

Console.WriteLine("\nthe smallest num: {0} the biggest num: {1}", min, max);
Console.ReadKey();
cramopy
  • 3,459
  • 6
  • 28
  • 42
  • You need to store the result of random.Next in a temporary variable and then check if your lotto array already contains that number. If yes continue, if not retry the random.next. This is more suited for a while loop instead of a for one – Steve Nov 07 '17 at 18:49
  • also a side-tip: consider using `++i` in your for loop. the resulting code will be (slightly) faster, because the value just gets increased, and not temporarly saved and then increased. just keep that in mind :) ps: this can savely be used for incrementation or decrementation in a for loop. when using it for other calcualtions or sth. else you have to take care that the leading operatos (de- or) increase first and the trailing operators returns the current value and then (de- or) increases after returning the current value. – cramopy Nov 07 '17 at 19:04

0 Answers0