PC Review


Reply
Thread Tools Rate Thread

Array random re-created mantains the same values

 
 
Pao
Guest
Posts: n/a
 
      15th Dec 2006
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???

 
Reply With Quote
 
 
 
 
Jon Shemitz
Guest
Posts: n/a
 
      15th Dec 2006
Pao wrote:

> 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?

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
 
Reply With Quote
 
Tom Porterfield
Guest
Posts: n/a
 
      15th Dec 2006
Pao wrote:
> 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[i] = 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[i] = 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.
--
Tom Porterfield

 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      15th Dec 2006
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.

--
Dave Sexton

"Jon Shemitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Pao wrote:
>
>> 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?
>
> --
>
> .NET 2.0 for Delphi Programmers
> www.midnightbeach.com/.net
> What you need to know.



 
Reply With Quote
 
Samuel R. Neff
Guest
Posts: n/a
 
      15th Dec 2006

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

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.


On 15 Dec 2006 11:57:59 -0800, "Pao" <(E-Mail Removed)> wrote:

>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???


 
Reply With Quote
 
Pao
Guest
Posts: n/a
 
      16th Dec 2006

Dave Sexton and Samuel R.Neff wrote:

>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.

 
Reply With Quote
 
sky19860822@gmail.com
Guest
Posts: n/a
 
      21st Dec 2006

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:(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting a list of individual values created with sum of values OssieMac Microsoft Excel Programming 6 30th Jan 2010 10:19 PM
Sorting random Data created from a random formula Six Sigma Blackbelt Microsoft Excel Misc 1 11th Sep 2008 11:03 PM
Array? Put values from random columns into 4 willwonka Microsoft Excel Worksheet Functions 4 16th Mar 2007 05:20 PM
score object property values against a decision rule created from from database table record values? hazz Microsoft VB .NET 3 3rd Jul 2005 06:52 PM
Comparing ARRAY elements with RANGE data and created a 2nd array JimP Microsoft Excel Worksheet Functions 2 10th Mar 2004 07:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:37 PM.