0
using System;
using System.Linq;

namespace Loto
{
    class Program
    {
        static void Main(string[] args)
        {
            short[] tirage = new short[6]; //array that contains the numbers
            short nbTirage; //the number randomed

            for (int i = 0; i < 6; i++)
            {
                nbTirage = NombreAleatoire();

                while (tirage.Contains(nbTirage)) //if the number has already been drawn
                {
                    nbTirage = NombreAleatoire(); //we random another one
                }

                Console.Write(nbTirage + " "); //writing the result
            }
        }

        static short NombreAleatoire()
        {
            Random nb = new Random();
            return (short)nb.Next(1, 49);
        }
    }
}

This is the full program.

It is supposed to draw 7 unique numbers between 1 and 49 included. The program works well in debug mode, but when i run it from the exe, it draw 7 times the same number. What is causing that ? I'm using Visual Studio.

rochb
  • 2,249
  • 18
  • 26
Lawliet
  • 21
  • 2
  • 1
    Have you tried rebuilding the program? – Needham May 12 '16 at 17:51
  • you need to keep one Random object and call Next on one object.. + I don't see filling "tirage" + you may remove namespace and class for brevity.. – rudolf_franek May 12 '16 at 17:53
  • Good luck trying to make a LOTTO Program you'll never win.. otherwise we all would have been rich a loooooooooong time ago.. plus use the debugger to step through your code and inspect the objects that you are creating and you will see immediately where the deficiencies are happening. – MethodMan May 12 '16 at 17:55
  • @MethodMan: The problem is specifically that the "error" *doesn't* happen when stepping through the code in debug mode. Stepping through the code doesn't really help when that approach physically can't manifest the problem. – David May 12 '16 at 17:57
  • that's because you are creating a new instance of `nb` incorrectly. – MethodMan May 12 '16 at 18:00

2 Answers2

5

Creating a new Random object in rapid succession gives every instance of that object the same (time-based) seed, so they will all generate the same number.

Use a single instance of Random to generate ongoing numbers. Something like this:

Random nb = new Random();
for (int i = 0; i < 6; i++)
{
    nbTirage = (short)nb.Next(1, 49);

    while (tirage.Contains(nbTirage)) //if the number has already been drawn
    {
        nbTirage = (short)nb.Next(1, 49); //we random another one
    }

    Console.Write(nbTirage + " "); //writing the result
}

(Note: You do experience this same behavior in debug mode, if you don't pause execution of the code. The seed for Random is based on the current time. So by pausing execution of the code you allow time to pass, thus changing the seed. Remove all breakpoints and let the application fully execute in debug mode and you'll see the same behavior that you see in release mode.)

David
  • 208,112
  • 36
  • 198
  • 279
2

This is because the default seed for Random is the time so you are seeding it the time each iteration. You would need to pass in the Random object to ensure you are getting a different number.

It is working in the debug step through because the time between iterations is much slower so the seed is changing.

Random nb = new Random();

for (int i = 0; i < 6; i++)
{
    nbTirage = NombreAleatoire(nb);

    while (tirage.Contains(nbTirage)) //if the number has already been drawn
    {
        nbTirage = NombreAleatoire(nb); //we random another one
    }

    Console.Write(nbTirage + " "); //writing the result
}

static short NombreAleatoire(Random nb)
{
    return (short)nb.Next(1, 49);
}
Matt Rowland
  • 4,575
  • 4
  • 25
  • 34