Always the same random numbers

  • Thread starter Thread starter Aspidus
  • Start date Start date
A

Aspidus

Hi,
I wrote a function that generates a ulong[]

I use it 3 times, one after the other, but it generates always the same
numbers.

At the end i get always 3 equal arrays.


Any idea?

Aspidus
 
Aspidus said:
Hi,
I wrote a function that generates a ulong[]

I use it 3 times, one after the other, but it generates always the same
numbers.

At the end i get always 3 equal arrays.


Any idea?

Aspidus

A few ideas, but it would be far better if you showed your code so that
I could give you a definitive answer instead of vague theories about it.
 
Aspidus said:
I wrote a function that generates a ulong[]

I use it 3 times, one after the other, but it generates always the same
numbers.

At the end i get always 3 equal arrays.

If you use Random class but keep newly constructing it with 'new
Random()' in a tight loop, you'll get the same values for a while. This
is because the initial state of the Random class comes from the system
clock, but a tight loop doesn't let the clock's value change.

Create a single Random class instance in a field, and use it, rather
than continuously creating new ones.

-- Barry
 
Sorry you're right, here it is:


idA = RndGen(LidA, W);
idB = RndGen(LidB, W);
Sh = RndGen(LSh, W);


private ulong[] RndGen(int NumCifre, int Base)
{
ulong[] Risultato = new ulong[NumCifre];
Thread.Sleep(15); //The generated arrays only differ if i
put this instruction, but in this way i loose time to generate more
arrays in few time (for statistic use)
Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));

for (int k = 0; k < NumCifre; k++)
{

if (k == 0 && Risultato[k] == 0)
Risultato[k] = (ulong)rnd.Next(1, (Base - 1)); /
else
Risultato[k] = (ulong)rnd.Next(Base - 1);
}

return Risultato;
}

Thank you. Aspidus.


Göran Andersson ha scritto:
Aspidus said:
Hi,
I wrote a function that generates a ulong[]

I use it 3 times, one after the other, but it generates always the
same numbers.

At the end i get always 3 equal arrays.


Any idea?

Aspidus

A few ideas, but it would be far better if you showed your code so that
I could give you a definitive answer instead of vague theories about it.
 
Sorry you're right, here it is:


idA = RndGen(LidA, W);
idB = RndGen(LidB, W);
Sh = RndGen(LSh, W);


private ulong[] RndGen(int NumCifre, int Base)
{
ulong[] Risultato = new ulong[NumCifre];
Thread.Sleep(15); //The generated arrays only differ if i
put this instruction, because the clock change its value, but in this
way i loose time to generate more arrays in few time (for statistic use)
Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));

for (int k = 0; k < NumCifre; k++)
{

if (k == 0 && Risultato[k] == 0)
Risultato[k] = (ulong)rnd.Next(1, (Base - 1)); /
else
Risultato[k] = (ulong)rnd.Next(Base - 1);
}

return Risultato;
}

Thank you. Aspidus.

Göran Andersson ha scritto:
Aspidus said:
Hi,
I wrote a function that generates a ulong[]

I use it 3 times, one after the other, but it generates always the
same numbers.

At the end i get always 3 equal arrays.


Any idea?

Aspidus

A few ideas, but it would be far better if you showed your code so that
I could give you a definitive answer instead of vague theories about it.
 
Aspidus said:
private ulong[] RndGen(int NumCifre, int Base)
{

Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));

Don't do this. Create a Random instance in a field, and use the field.

Also, just using 'new Random()' will essentially do what you are doing
here, initializing it from the system clock. You don't need to pass the
ticks manually.

-- Barry
 
I did it ;-) thank you! It works!

Random rnd = new Random();
idA = RndGen(LidA, W, rnd);
idB = RndGen(LidB, W, rnd);
Sh = RndGen(LSh, W, rnd);




private ulong[] RndGen(int NumCifre, int Base, Random rnd)
{
ulong[] Risultato = new ulong[NumCifre];
for (int k = 0; k < NumCifre; k++)
{
if (k == 0 && Risultato[k] == 0)
Risultato[k] = (ulong)rnd.Next(1, (Base - 1));
else
Risultato[k] = (ulong)rnd.Next(Base - 1);
}

return Risultato;
}



Bye Aspidus.




Barry Kelly ha scritto:
Aspidus said:
private ulong[] RndGen(int NumCifre, int Base)
{

Random rnd = new Random(unchecked((int)DateTime.Now.Ticks));

Don't do this. Create a Random instance in a field, and use the field.

Also, just using 'new Random()' will essentially do what you are doing
here, initializing it from the system clock. You don't need to pass the
ticks manually.

-- Barry
 

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

Back
Top