Array random re-created mantains the same values

P

Pao

My code works in this way:
I declared a static array in a class (public static int[] GVetRandom =
new int[3];)
that in a for cycle I fill with random numbers.
The array gets cleared (clear method) and refilled at each turn of
cycle.

On my developing pc, the same sequence of random numbers was repeated
from on turn of cycle to the other; I put some Application.DoEvents()
and all gone well.

But in the customer's pc, the array are generated exactly the same
yet, it gives 2 or 3 duplicates
for example:
1st turn in teh for: the array is 10-56-33
2nd turn in teh for: the array is 34-22-54
3rd turn in teh for: the array is 34-22-54
4th turn in teh for: the array is 34-22-54
5th turn in teh for: the array is 65-31-24

what could it be???
 
J

Jon Shemitz

Pao said:
But in the customer's pc, the array are generated exactly the same
yet, it gives 2 or 3 duplicates
for example:
1st turn in teh for: the array is 10-56-33
2nd turn in teh for: the array is 34-22-54
3rd turn in teh for: the array is 34-22-54
4th turn in teh for: the array is 34-22-54
5th turn in teh for: the array is 65-31-24

what could it be???

Dunno. Why don't you post an actual code snippet?
 
T

Tom Porterfield

Pao said:
My code works in this way:
I declared a static array in a class (public static int[] GVetRandom =
new int[3];)
that in a for cycle I fill with random numbers.
The array gets cleared (clear method) and refilled at each turn of
cycle.

On my developing pc, the same sequence of random numbers was repeated
from on turn of cycle to the other; I put some Application.DoEvents()
and all gone well.

But in the customer's pc, the array are generated exactly the same
yet, it gives 2 or 3 duplicates
for example:
1st turn in teh for: the array is 10-56-33
2nd turn in teh for: the array is 34-22-54
3rd turn in teh for: the array is 34-22-54
4th turn in teh for: the array is 34-22-54
5th turn in teh for: the array is 65-31-24

what could it be???

What are you using as the seed for your random numbers? A guess is that
possibl you are using a date or time that isn't precise enough. A random
number generator with the same seed will produce the same "random" numbers.
Take a look at the following examples:

// this will produce the same results several times through the loop
for (int h = 0; h < 100; h++)
{
DateTime now = DateTime.Now;
int seed = now.Second * 1000 + now.Millisecond;
Random random = new Random(seed);
int[] rand = new int[3];
for (int i = 0; i < rand.Length; i++)
{
rand = random.Next(0, 100);
}
Console.WriteLine("{0}-{1}-{2}", rand[0], rand[1], rand[2]);
}

// this will be much more random than the first example
DateTime now = DateTime.Now;
int seed = now.Second * 1000 + now.Millisecond;
Random random = new Random(seed);
for (int h = 0; h < 100; h++)
{
int[] rand = new int[3];
for (int i = 0; i < rand.Length; i++)
{
rand = random.Next(0, 100);
}
Console.WriteLine("{0}-{1}-{2}", rand[0], rand[1], rand[2]);
}


If that's not it, if you post a sample code that reproduces the problem, no
doubt someone can point out the cause.
 
D

Dave Sexton

Hi,

I'd like to wager a guess that you're recreating a new Random object each
time the array is generated. Instead, keep a single read-only instance of a
Random object in a field and use that each time.
 
S

Samuel R. Neff

Don't don't instantiate a random number generator each tiem through
the loop--create one once and reuse that throughout the lifetime of
the application.

HTH,

Sam
 
P

Pao

Dave said:
Don't don't instantiate a random number generator each tiem through
the loop--create one once and reuse that throughout the lifetime of
the application.


Thank you all very much, expecially Dave and Samuel.
I tried their suggestion above and all works now.
 
S

sky19860822

I want to know what is some RSA security's product using as the seed
for its random numbers or how I can find their product's
whitepaper----if it does exist.
My object is to find some ways of producing random numbers.
I will appreciate you if you send somethings om that tome.
My email:[email protected]
 

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