0

I'm trying to create a file with 1000 line of random numbers separated by space. This is just to simulate the numbers of a lotto machine. I create a class that will handle the combination of the six number in an array of 6 int.

class cTicket
    {
        //Propiedades
        private volatile int[] p_nums;

    //Constructor
    public cTicket()
    {

    }

    //Metodos

    public void setTicket(int[] o_nums)
    {
        p_nums = o_nums;
    }

    public int[] getTicket()
    {
        return p_nums;
    }

}

like I said I need to create a combination of 1000 lines with an array of 6 number selected in a random way. So at some point of the code I got something like this:

 private void button2_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < 1000; i++) // Bucle de 1,000 combinaciones
        {
            objTicket.setTicket(objGenerador.generaNumero());
            //MessageBox.Show(integerArrayString(objTicket.getTicket()));
            write.WriteLine(i + integerArrayString(objTicket.getTicket()));
        }
        write.Close(); // Cierra operacion de escritura del archivo

    }

But this part is not working at all. It repeat the same combination of 6 numbers over and over again. Only change like three times the whole process, another thing that I notice is that if I put the output in a message box it work perfect. But not when I try to write it into the file.

I hope someone can help with my problem.

Thanks.

Hi, thanks again, after reading the post below and get more documentation i get to the seed of the problem and it was the way that i generate the ramdom numbers as you guy says.

public int[] generaNumero() {

        Random obj = new Random(); 
        int v_idx;
        for (int x = 0; x < 6; x++)
        {
            v_idx = x;
            v_array[x] = obj.Next(1, 38);

            while (v_idx >= 0)
            {
                if (v_array[x] == v_array[v_idx])
                {
                    v_array[x] = obj.Next(1, 38);
                    v_idx = x;
                }
                v_idx--;
            }
        }

        return v_array;

i just simple remove the line were i do the intance "Random obj = new Random();" from this method and its working perfect now.

thanks a lot.

  • Random number generators use a seed value to generate pseudo-random numbers. If you don't change the seed you will get the same list. See the Remarks section on MSDN - [Random Class](http://msdn.microsoft.com/en-us/library/system.random.aspx) – Tim Apr 22 '13 at 08:43
  • 1
    A wild guess: you create the `Random` class in `generaNumero` method. Try to move it to the constructor of `objGenerador` http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – I4V Apr 22 '13 at 08:47

1 Answers1

1

your random generating function is very possibly not generating real randoms.

i Imagine you create per call a new Random (which use all the same time as seed) so you always get the same "random" number collection

post your objGenerador.generaNumero() method

How to use the Random class: MSDN

check the example they show, there you can see that two random's created a the same time produce the same output.

private static Random _Random = new Random();
public int[] generaNumero() 
{
 int v_idx; 
 for (int x = 0; x < 6; x++) 
 { 
  v_idx = x; 
  v_array[x] = _Random.Next(1, 38); 
  while (v_idx >= 0) 
  { 
   if (v_array[x] == v_array[v_idx]) 
   { 
    v_array[x] = _Random.Next(1, 38); 
    v_idx = x; 
   } 
   v_idx--; 
  } 
 } 
 return v_array; 
} 
fixagon
  • 5,506
  • 22
  • 26
  • public int[] generaNumero() {Random obj = new Random(); // Objeto Randomico int v_idx; for (int x = 0; x < 6; x++) { v_idx = x; v_array[x] = obj.Next(1, 38); while (v_idx >= 0) { if (v_array[x] == v_array[v_idx]) { v_array[x] = obj.Next(1, 38); v_idx = x; } v_idx--; } } return v_array; } – user2306571 Apr 23 '13 at 01:43
  • make the Random object static --> it gets initialised once and whenever you want a new random number you consume it with random.Next – fixagon Apr 23 '13 at 07:27