473,395 Members | 1,763 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,395 software developers and data experts.

Random Numbers are the same!

I am trying to generate 2 random numbers that are diffrent, in order to
add them to existing numbers to generate numbers that start out the
same, but are randomly added and subtracted so that they can go down
similar paths, but not be the same. I will implement code later to make
sure i they go more than 10 apart from each other that they are moved
closer together, but this is what I have so far, and when the program
is run, the two random numbers (RA, RB) end up being the same,
resulting in these 2 numbers always being Identicle, how can I prevent
this?

private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
int X=0;
int Y=0;
private void button1_Click(object sender, System.EventArgs e)
{
int RA=RandomNumber(-3,4);
int RB=RandomNumber(-2,5)-1;
X=X+RA;
Y=Y+RB;
if (X<=0 && Y<=0)
{
X=0;
Y=0;
}
else if (Y<=0)
{
Y=0;
}
else if (X<=0)
{
X=0;
}
lblA.Text=X.ToString();
lblB.Text=Y.ToString();
}

Nov 17 '05 #1
5 2380
You need to "seed" the generator.

new Random(unchecked((int)DateTime.Now.Ticks));
<cv****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I am trying to generate 2 random numbers that are diffrent, in order to
add them to existing numbers to generate numbers that start out the
same, but are randomly added and subtracted so that they can go down
similar paths, but not be the same. I will implement code later to make
sure i they go more than 10 apart from each other that they are moved
closer together, but this is what I have so far, and when the program
is run, the two random numbers (RA, RB) end up being the same,
resulting in these 2 numbers always being Identicle, how can I prevent
this?

private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
int X=0;
int Y=0;
private void button1_Click(object sender, System.EventArgs e)
{
int RA=RandomNumber(-3,4);
int RB=RandomNumber(-2,5)-1;
X=X+RA;
Y=Y+RB;
if (X<=0 && Y<=0)
{
X=0;
Y=0;
}
else if (Y<=0)
{
Y=0;
}
else if (X<=0)
{
X=0;
}
lblA.Text=X.ToString();
lblB.Text=Y.ToString();
}

Nov 17 '05 #2
hmmm, since both numbers are generated at exactly the same time, I
think I need to find another way of generating a new seed, since using
the code snippit you provided doesn't seem to eliviate the situation.

Nov 17 '05 #3
You need to create a Random only once, and then call the same instance
repeatedly to get different random numbers. In some class, (e.g. Foo)
declare
public static Random myRandom = new Random();

then in your private int RandomNumber(int min, int max), omit the
declaration of random, and have:
return Foo.myRandom.Next(min, max);
<cv****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I am trying to generate 2 random numbers that are diffrent, in order to
add them to existing numbers to generate numbers that start out the
same, but are randomly added and subtracted so that they can go down
similar paths, but not be the same. I will implement code later to make
sure i they go more than 10 apart from each other that they are moved
closer together, but this is what I have so far, and when the program
is run, the two random numbers (RA, RB) end up being the same,
resulting in these 2 numbers always being Identicle, how can I prevent
this?

private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
int X=0;
int Y=0;
private void button1_Click(object sender, System.EventArgs e)
{
int RA=RandomNumber(-3,4);
int RB=RandomNumber(-2,5)-1;
X=X+RA;
Y=Y+RB;
if (X<=0 && Y<=0)
{
X=0;
Y=0;
}
else if (Y<=0)
{
Y=0;
}
else if (X<=0)
{
X=0;
}
lblA.Text=X.ToString();
lblB.Text=Y.ToString();
}

Nov 17 '05 #4
Thanks! this worked like what I was striving for, thanks so much!

Nov 17 '05 #5
How about maintaining a global instance of the Random class, seeding it
once, and then you should get random numbers each time.

"cvncpu" <cv****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hmmm, since both numbers are generated at exactly the same time, I
think I need to find another way of generating a new seed, since using
the code snippit you provided doesn't seem to eliviate the situation.

Nov 17 '05 #6

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

Similar topics

4
by: james blair | last post by:
Hi I am generating a random number using random.randint(1,10000000000) Whats the possibility that the numbers generated will be same when generated by 100 users at the same time? Whats the best...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
25
by: JNY | last post by:
I am using random to generate random numbers, thus: int x,y; for (y = 0;y < 5;y++) { x = random(50); cout << x; }
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
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...
5
by: Muffin | last post by:
I hope somebody can show me why I need to have a messagebox to get "random" numbers in my example. If I comment out the message box call that is in RollAbility() the numbers generated are not...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.