473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Always the same random numbers

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
Mar 4 '07 #1
8 5670
Aspidus wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 4 '07 #2
Aspidus wrote:
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

--
http://barrkel.blogspot.com/
Mar 4 '07 #3
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 wrote:
>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.
Mar 4 '07 #4
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 wrote:
>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.
Mar 4 '07 #5
Aspidus wrote:
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

--
http://barrkel.blogspot.com/
Mar 4 '07 #6
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 wrote:
>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
Mar 4 '07 #7
JR
http://xkcd.com/c221.html

JR
"Aspidus" <as*****@interfree.it???
??????:45***********************@reader3.news.tin. it...
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

Mar 4 '07 #8
Aspidus <as*****@interfree.itwrote:
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?
See http://pobox.com/~skeet/csharp/miscu...ticrandom.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 5 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
6
by: ad | last post by:
I write a function below the generate a alpha, but when I call it , it always return 'b' How can I do generate a character randomly. public static char CharRnd() { Random rnd = new...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
6
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
20
by: A | last post by:
Hi all. Is this a bug or what??? here is a simple code: <?php mt_srand(1); echo mt_rand(0, 255)."<br />"; echo mt_rand(0, 255)."<br />";
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.