0

My WinForms lottery program asks for 8 numbers. First 7 of them are lottery picks, the last one represents number of spins. After clicking on the "Start Button", user can see how many times did he score 5, 6 or 7 matches. The problem lies in these results, they are very inconsistent. I can get for example 0-5s, 46223-6s and 0-7s in 999999 spins after 1st try, 2nd try: 12512-5s, 5002-6s, 0-7s and 3rd try i got 3 zeros(0 0 0). I went through the code with Debugger(working in Visual Studio) and there should no longer be any problems with doubles. To be honest i don't know what is wrong.

Here comes the code:

for (int n = 0; n < spins; n++)
{
    List<string> rand = func.randomizer();
    rand = func.compare(numb, rand);
    if (rand.Count == 2) five++;
    if (rand.Count == 1) six++;
    if (rand.Count == 0) seven++;
{

and randomizer and compare:

public List<string> randomizer()        
{
        ArrayList randNumb = new ArrayList();                
        Random generator = new Random();
        int randomNumb;
        List<string> rand = new List<string>();
        for (int i = 0; i < 7; i++)
        {
            do
            {
                randomNumb = generator.Next(1, 36);
            }while (randNumb.Contains(randomNumb));
            randNumb.Add(randomNumb);
            rand.Add(randomNumb.ToString());
        }
        return rand;
    }
    public List<string> compare(List<string> numb, List<string> rand)     
    {
        rand.Remove(numb[0]);
        rand.Remove(numb[1]);
        rand.Remove(numb[2]);
        rand.Remove(numb[3]);
        rand.Remove(numb[4]);
        rand.Remove(numb[5]);
        rand.Remove(numb[6]);
        return rand;
    }

Thanks for help. /bow

N... K...
  • 1
  • 1
  • Where does `numb` in your first code block come from? – Renardo Mar 09 '18 at 19:00
  • That's not the whole code, these are just parts which i THINK are wrong. – N... K... Mar 09 '18 at 19:03
  • numb is a list containing users picks(7 numbers ranging 1-35) – N... K... Mar 09 '18 at 19:04
  • @Sudsy1002: If `numb`were user input, where would the 999.999 spins come from? If `numb` is generated *randomly* it might be that there is a correlation between the two (pseudo) random data series. Then the results of the program might be totally predictable (if no external entropy source, such as clock cycles, is used). – Renardo Mar 09 '18 at 19:05
  • @Sudsy1002 O.k. So the whole thing is due to initialising a new random generator for each spin. – Renardo Mar 09 '18 at 19:09
  • numb are user input and they stay the same throughout the for-loop, spins is a user input as well and 999999 is just an example use. Spins don't have to be 999999, it can be less. – N... K... Mar 09 '18 at 19:13
  • 2
    First stop using `ArrayList` and switch over to the generic `List` for type safety. Second if you want 7 unique random numbers between 1 and 36 you'd be better off doing a Fisher-Yates shuffle where you remove random number from a set to avoid the loop that keeps checking if the next random number was already selected. Finally you need to declare your `Random` instance at a higher level as it is seeded with the current date time and you're using it inside of a tight loop that will result in each instance you create having the same seed and thus the same set of random numbers. – juharr Mar 09 '18 at 19:18

0 Answers0