2

Ive got a class Population with its constructor:

private Individual[] m_population;
    public Population() {
        m_population = new Individual[POP_SIZE];
        for (int i = 0; i < POP_SIZE; i++) {
            m_population[i] = new Individual();
            m_population[i].randGenes();
        }
        //some other code here
    }

Inside the Individual class I have the method for randGenes():

Random rand2 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);

        public void randGenes()
        {
            for (int i = 0; i < SIZE; ++i)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    this.setGene(i, j, rand2.Next(1));
                }
            }
        }

I just need a set of 25 radnom 1s and 0s for each of the 10 individuals. But I always get the same set for each of the 10 individuals, when I fiddle even more with it I only get 0s. Its only when I enter debug mode that I get truly random numbers. I see everywhere that the new random should be created outside the method and/or the loop, which I did so I dont know what to do next?

EDIT My question wasn't a duplicate of that question because the answer provided there did not help me. What did help me however was the answer which I awarded below.

Vrankela
  • 1,162
  • 3
  • 16
  • 39

2 Answers2

2

It is because you are effectively creating a new instance of Random in such a short period of time, that Random is not able to use a different seed - which is based off the current time (quite explicitly, as in your example). The reason it works when in debug mode is because the overhead of the debugger is making your code run slower, which means an amount of time has passed between creating instances of random, which then means it can seed with a different value. (Eg, the same seed will always produce the same set of random numbers). You should create a static variable to store an instance of Random, and use the same instance of random everywhere in your program. You should be able to change your random to

static Random rand2 = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);

and it should begin to work.

Warrick
  • 1,623
  • 1
  • 17
  • 20
  • That solved it, I did try using static random but I didnt use the datetime as seed... – Vrankela Jul 04 '16 at 07:18
  • You probably should omit the DateTime seed completely, since this is the default behavior anyway. Good to hear it's working for you now anyway :) – Warrick Jul 04 '16 at 07:43
1

rand2 appears to be an instance variable, so you create a new random number generator for every object. Since Ticks is a number in milliseconds, if your code executes fast enough, the seed will be the same for every Individual, and the RNGs all return the same sequence.

This doesn't happen when you break into the debugger because you introduce an artificial delay.

Either use the same Random object for every Individual, or seed the RNG differently.

zneak
  • 134,922
  • 42
  • 253
  • 328