0

I made a program to generate 7 random numbers for a lottery using a array. I have generated a random number between 1, 50 but every number shows in order and not on the same line. I would also like to store the auto generated numbers in a array to use. I am not sure how to fix this any help would be appreciated

static void AutoGenrateNumbers()
{
    int temp;
    int number = 0;
    int[] lotto = new int[7];

    Random rand = new Random();

    for (int i = 0; i <= 50; i++)
    {
        number = 0;
        temp = rand.Next(1, 50);

        while (number <= i)
        {
            if (temp == number)
            {
                number = 0;
                temp = rand.Next(1, 50);
            }
            else
            {
                number++;
            }
        }

        temp = number;
        Console.WriteLine($"the new lotto winning numbers are:{number}Bonus:{number}");
    }
}
maccettura
  • 10,514
  • 3
  • 28
  • 35
andrew197
  • 27
  • 5
  • You are using `WriteLine()` on every iteration of the loop to write to the console, if you want them all on one line then either build a string in the loop and then output it once after, or use `Write()` inside the loop – maccettura Nov 14 '19 at 17:45
  • Also, do you want all the numbers to be unique (i.e no duplicate numbers)? – maccettura Nov 14 '19 at 17:50
  • I do want the numbers to be unique – andrew197 Nov 14 '19 at 17:55

3 Answers3

1

Is this what you need?

static void AutoGenrateNumbers()
{
    int temp;
    int[] lotto = new int[7];

    Random rand = new Random();

    for (int i = 0; i < 7; i++)
    {
        temp = rand.Next(1, 50);
        lotto[i]= temp;
    }
    Console.Write($"the new lotto winning numbers are: ");

    for (int i = 0; i < 6; i++)
    {
        Console.Write(lotto[i]+" ");
    }
    Console.Write($"Bonus:{lotto[6]}");

}

edit: if you want the numbers to be unique:

static void AutoGenrateNumbers()
{
    int temp;
    int[] lotto = new int[7];

    Random rand = new Random();

    for (int i = 0; i < 7; i++)
    {
        do
        {
            temp = rand.Next(1, 50);
        }
        while (lotto.Contains(temp));
        lotto[i]= temp;
    }

    Console.Write($"the new lotto winning numbers are: ");
    for (int i = 0; i < 6; i++)
    {
        Console.Write(lotto[i]+" ");
    }
    Console.Write($"Bonus:{lotto[6]}");

}
andi475
  • 69
  • 3
  • 2
    Your technique for uniqueness works for small numbers like 7, but in general is not a good technique as the size gets larger. The better technique is *make an array of 50 elements, sort it by a random key, and take the first seven elements*. – Eric Lippert Nov 14 '19 at 19:24
1

A better way to do this is just to generate all the numbers 1-50, shuffle them and then just take 7. Using Jon Skeet's Shuffle extension method found here:

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
    T[] elements = source.ToArray();
    for (int i = elements.Length - 1; i >= 0; i--)
    {
        int swapIndex = rng.Next(i + 1);
        yield return elements[swapIndex];
        elements[swapIndex] = elements[i];
    }
}

Now your code is very simple:

static void AutoGenrateNumbers()
{
    var lotto = Enumerable.Range(0, 50).Shuffle(new Random()).Take(7);
    Console.WriteLine("the new lotto winning numbers are: {0}", string.Join(",", lotto));
}

Fiddle here

maccettura
  • 10,514
  • 3
  • 28
  • 35
0

Just to add to the existing answers tried to do that in one LINQ statement:

static void Main(string[] args)
{
    var rand = new Random();
    Enumerable
        .Range(1, 7)
        .Aggregate(new List<int>(), (x, y) =>
        {
            var num = rand.Next(1, 51);
            while (x.Contains(num))
            {
                num = rand.Next(1, 51);
            }
            x.Add(num);
            return x;
        })
        .ForEach(x => Console.Write($"{x} "));
}

The result is something like:

34 24 46 27 11 17 2
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68