creating an array of objects

G

Guest

I use Visual Studio .NET 2002 for developing such a program

(...
public Population(int quantity, int gntpLength

currentPop = new Person[quantity]

for (int i = 0; i < quantity; i++

currentPop = new Person(gntpLength)
// * Console.WriteLine(currentPop.ToString() + "\n")


(...

This is a constructor of Population class, where currentPop is its field (Person[] currentPop; - a reference to an array of Person objects). The constructor of Person class returns a Person instance, which is everytime different (this is because I use Random.Next() method to create Person objects). But when I create an array of Person objects in the way above (without the line marked (*)), every instance of Person in the array is the same. It looks like Person constructor would be called only once (instead of 'quantity' number of times). However, when I added the marked line everything was fine - every instance of Person in currentPop array was different. Why it so happens
In addition when I debugged the program and executed it line by line (even without the marked line) it worked OK. But when I set the breakpoint after creating the 'currentPop' array it was wrong. Does it happen because of setting some compiling option or maybe a compiler isn't working fine? Please help me understand why it so happens. Thanks in advance.
 
M

Morten Wennevik

Hi sebastian,

How do you use Random?
If you reinitialize it very fast it can produce the same numbers, where
the slower step by step debugging would not.
 
J

Jon Skeet [C# MVP]

sebastian said:
I use Visual Studio .NET 2002 for developing such a program:

(...)
public Population(int quantity, int gntpLength)
{
currentPop = new Person[quantity];

for (int i = 0; i < quantity; i++)
{
currentPop = new Person(gntpLength);
// * Console.WriteLine(currentPop.ToString() + "\n");
}
}
(...)

This is a constructor of Population class, where currentPop is its
field (Person[] currentPop; - a reference to an array of Person
objects). The constructor of Person class returns a Person instance,
which is everytime different (this is because I use Random.Next()
method to create Person objects). But when I create an array of
Person objects in the way above (without the line marked (*)), every
instance of Person in the array is the same. It looks like
Personconstructor would be called only once (instead of 'quantity'
number of times). However, when I added the marked line everything
was fine - every instance of Person in currentPop array was
different. Why it so happens? In addition when I debugged the program
and executed it line by line (even without the marked line) it worked
OK. But when I set the breakpoint after creating the 'currentPop'
array it was wrong. Does it happen because of setting some compiling
option or maybe a compiler isn't working fine? Please help me
understand why it so happens. Thanks in advance.


It sounds like when you're debugging, something very strange is going
on (like it's debugging the wrong code). However, if you can produce a
short but *complete* example program which demonstrates the problem, it
should be easy to find out what's going wrong.

See http://www.pobox.com/~skeet/csharp/complete.html for what I mean.
 
S

SleazySt

It sounds like you create a new Random object in the constructor for Person. This Random object is,
by default, seeded with a value based on the current time. If you run this without the
Console.WriteLine and without debugging, it runs so fast that this seed value never changes and you
get the same random number every time.
What you should do is create a new Random object in as a static member of Person, and use that in
the constructor.

sebastian said:
I use Visual Studio .NET 2002 for developing such a program:

(...)
public Population(int quantity, int gntpLength)
{
currentPop = new Person[quantity];

for (int i = 0; i < quantity; i++)
{
currentPop = new Person(gntpLength);
// * Console.WriteLine(currentPop.ToString() + "\n");
}
}
(...)

This is a constructor of Population class, where currentPop is its field (Person[] currentPop; - a

reference to an array of Person objects). The constructor of Person class returns a Person instance,
which is everytime different (this is because I use Random.Next() method to create Person objects).
But when I create an array of Person objects in the way above (without the line marked (*)), every
instance of Person in the array is the same. It looks like Person constructor would be called only
once (instead of 'quantity' number of times). However, when I added the marked line everything was
fine - every instance of Person in currentPop array was different. Why it so happens?
In addition when I debugged the program and executed it line by line (even without the marked
line) it worked OK. But when I set the breakpoint after creating the 'currentPop' array it was
wrong. Does it happen because of setting some compiling option or maybe a compiler isn't working
fine? Please help me understand why it so happens. Thanks in advance.
 
G

Guest

This is all the relevant code

class Populatio

Person[] currentPop

public Population(int quantity, int gntpLength

currentPop = new Person[quantity]

for (int i = 0; i < quantity; i++

currentPop = new Person(gntpLength)
// Console.WriteLine(currentPop.ToString() + "\n")


(...
class Perso

Genotype gntp
int fitValue

public Person(int gntpLength

gntp = new Genotype(gntpLength)
fitValue = 0


(...

class Genotyp

int[] chromosome

public Genotype(int gntpLength

chromosome = new int[gntpLength]
Random rand = new Random()
for (int i = 0; i < gntpLength; i++

chromosome = rand.Next(0, 2)



Thank you all!!!
 
G

Guest

Thank you all so much. I didn't know how the Random object worked but defining it as static solved the problem
Greetings.
 
J

Jon Skeet [C# MVP]

sebastian said:
This is all the relevant code:

<snip>

For future reference, it's much more helpful if you provide code which
can be cut, pasted, compiled, and run, all in the space of seconds.
People helping you shouldn't have to add their own Main methods etc.
However, this time it's unnecessary:
Random rand = new Random();

This line is your problem. You're creating the random number generator
freshly each time, so it'll take the "current time" as the seed each
time - meaning that if you create Genotypes several times in quick
succession, they'll have the same data.

Instead, use a static variable to hold Random, and either make it
thread-static so you get one per thread or use locking to make sure you
don't access it from more than one thread at a time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top