Rundom function
-
Hi, I'm trying to create a function that gets 2 numbers and returns a random number between those two number. help please
The simplest answer is to look at the System.Random class. Wrap one in a class and provide the method you need.
-
Hi, I'm trying to create a function that gets 2 numbers and returns a random number between those two number. help please
-
The simplest answer is to look at the System.Random class. Wrap one in a class and provide the method you need.
you are to kind: you also could tell him to use the feature "object browser" with the "random" keyword :)
I cannot remember: What did I before google?
-
you are to kind: you also could tell him to use the feature "object browser" with the "random" keyword :)
I cannot remember: What did I before google?
He may have tried that with "rundom". :rolleyes: I expect he'll be back asking why it keeps giving the same value over and over.
-
Don't you think you should have stated that in your original question then? We don't read minds.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Hi, I'm trying to create a function that gets 2 numbers and returns a random number between those two number. help please
You'll need a source of randomness and a way to map the output of that source to numbers between your two inputs. If you want the result to be uniformly chosen from the specified interval, you shouldn't use modulo - that would result in a "first part" where the numbers have a slightly higher chance of being picked then the ones in the "second part". edit: Ok I just saw your post that you don't want to use the random class, then look up Mersenne Twister and implement that if you want it to be useful, or any simple PRNG if you want something as bad as System.Random or worse.
-
Try this: Simple Random Number Generation[^] And, as Peter said, why did you not say this in the original post since it would avoid people telling you to look it up in help.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
He may have tried that with "rundom". :rolleyes: I expect he'll be back asking why it keeps giving the same value over and over.
you're an optimist. I think there is no running code yet. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
treuveni wrote:
I don't want to use the random class.
i want to ceate this by my own
Well why didn't you f*ckin say that in your original message? How are we supposed to read your simple little mind when it generates almost no electrical energy on its own?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
treuveni wrote:
I don't want to use the random class.
i want to ceate this by my own
Well why didn't you f*ckin say that in your original message? How are we supposed to read your simple little mind when it generates almost no electrical energy on its own?
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
Hi, I'm trying to create a function that gets 2 numbers and returns a random number between those two number. help please
Just use the Sytem.Random class, assign random numbers to two integer variables and in the if statement make sure that you create another random number by checking the statement to be between the first two random numbers. but you need to make sure if both are positive or one is negative. You will get help on this over here: - http://www.javadocexamples.com/java/util/java.util.Random.html[^] Javadoc is the most helpful thing for any java developers. So always visit javadoc and try to find your answers
-
public int GetRandomNumber()
{
//this must be random because it was determined by fair dice rolling
return 3;
} -
Just use the Sytem.Random class, assign random numbers to two integer variables and in the if statement make sure that you create another random number by checking the statement to be between the first two random numbers. but you need to make sure if both are positive or one is negative. You will get help on this over here: - http://www.javadocexamples.com/java/util/java.util.Random.html[^] Javadoc is the most helpful thing for any java developers. So always visit javadoc and try to find your answers
A couple of points. First of all, you point him to System.Random, and then you send him over to the Java documentation - this is an inconsistent approach. Secondly, he's stated elsewhere in the thread that he wants to roll his own random implementation (it's probably homework). Finally, resources like MSDN are not always the best solution - they tend to be written by tech writers rather than battle hardened developers working with the technologies out in the trenches.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Just use the Sytem.Random class, assign random numbers to two integer variables and in the if statement make sure that you create another random number by checking the statement to be between the first two random numbers. but you need to make sure if both are positive or one is negative. You will get help on this over here: - http://www.javadocexamples.com/java/util/java.util.Random.html[^] Javadoc is the most helpful thing for any java developers. So always visit javadoc and try to find your answers
-
A couple of points. First of all, you point him to System.Random, and then you send him over to the Java documentation - this is an inconsistent approach. Secondly, he's stated elsewhere in the thread that he wants to roll his own random implementation (it's probably homework). Finally, resources like MSDN are not always the best solution - they tend to be written by tech writers rather than battle hardened developers working with the technologies out in the trenches.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
I apologize. My bad.... And I agree to the MSDN is not always the best but for beginners, it does provide a good start.... After understanding the codes on MSDN they can always try and improvise...