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(in t min, int max)
{
Random random = new Random();
return random.Next(min , max);
}
int X=0;
int Y=0;
private void button1_Click(o bject sender, System.EventArg s 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.ToS tring();
lblB.Text=Y.ToS tring();
} 5 2399
You need to "seed" the generator.
new Random(unchecke d((int)DateTime .Now.Ticks));
<cv****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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(in t min, int max) { Random random = new Random(); return random.Next(min , max); } int X=0; int Y=0; private void button1_Click(o bject sender, System.EventArg s 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.ToS tring(); lblB.Text=Y.ToS tring(); }
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.
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(in t min, int max), omit the
declaration of random, and have:
return Foo.myRandom.Ne xt(min, max);
<cv****@gmail.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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(in t min, int max) { Random random = new Random(); return random.Next(min , max); } int X=0; int Y=0; private void button1_Click(o bject sender, System.EventArg s 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.ToS tring(); lblB.Text=Y.ToS tring(); }
Thanks! this worked like what I was striving for, thanks so much!
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.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 method to generate random numbers so that they are most
likely unique??
Thanks
|
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 spots on the
webpage. with a delay timer on them, so they keep changing as the page is
open. Not random each time the page is loaded.
If anyone can help it would be greatly appreaciated, I have tried many of
|
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;
}
|
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 generate a set of
random number that follow a normal distribution N(0,1) ? I am develloping
on power4 machine running AIX.
Thank you for your help
|
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 better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.
| |
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.
|
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 random , otherwise it works
fine. I really would like to get rid of the messagebox call.
thx
static class Die
{
|
by: chico_yallin |
last post by:
I just wana make a random id number based on4 digits-for examples??
Thanks in Advance
Ch.Yallin
|
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 that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |