473,774 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5693
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(unchecke d((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(unchecke d((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(unchecke d((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(unchecke d((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*****@interf ree.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*****@interf ree.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.co m>
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
11960
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 using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a
13
4236
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 between 0 and 100? (call rand() only 10 times...) Thanks, qq
12
5230
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 RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
6
13085
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 Random(); char c = (char)rnd.Next('a', 'z' + 1);
22
3455
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
2125
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 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.
3
2085
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: ************************************************* Function RandomString(size as integer, lowerCase as boolean) as string Dim builder as StringBuilder = new StringBuilder() Dim random as Random = new Random() Dim i as integer dim ch as char
24
7234
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 uniformly distributed and unique numbers ?
20
5136
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
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.