I want to make a game that involves random numbers (between 1 and 2(as in either 1 or 2)) Please help me. (or maybe 1 and 4)
Number from 1 to 2:
int number = 1 + (int) (Math.random() * 2.0);
I to 4:
int number = 1 + (int) (Math.random() * 4.0);
Ok, iâl add that in. ;D
or
public int randomNumber(int min, int max){
int random;
do{
random = Math.random * max;
}while(random < min)
return random;
}
Without loop:
public int randomNumber(int min, int max)
{
int random = min + (int)(Math.random * (max-min+1));
return random;
}
even simpler
Random random = new Random();
int result_for_two = 1 + random.nextInt(2); //random.nextInt(2) will give either
//0 or 1, so we add 1 to it making 0 = 1 and 1 = 2
//exact same reason as result_for_two we want to get off 0.
int result_for_four = 1 + random.nextInt(4);
public int randomNumber(int min, int max)
{
return min + (int)(Math.random * (max-min+1));
}
battle for the random numbers!
How many different ways can we produce the number!
PoorlyâŚmany ways!
Do you mean many ways, but poorly? Or poor ways but many ways?
Lolwut
Anyways I think you should Google this stuff first. Or at least learn more JavaâŚI mean, it really wouldnât take you very long to search this up.
@masteryoom: I meant there are many ways to poorly generate. This is it self isnât a problem as poor quality (assuming you donât massively skew the distribution) is more than sufficient virtually always in the context of games. However there are some âbad habitsâ which should be avoided IHMO. Iâll try to motive myself to toss together a basic wiki page.
If you stick to the API of class Random, I assume youâre safe from most pitfalls - I assume tha makers of the class knew what they were doing. And the Java API has RNG implementation which suffice data encrytion standard, that ensures a certain level of quality ârandomnessâ.
Just look what methods are there and use them.
Do not mess with the numbers much by yourself unless you have a good understandig of math and what random numbers are about (i.e. which qualities make up a âgoodâ random number, and what srt of calculations will damage those qualities.)
Random is actually an awful design. Not really a problem if youâre single threaded and arenât generating a large number random numbers though.
Really? Iâm using it to generate all the stellar systems in my âSolarexâ project, and it seems to be quite alright. Itâs single threaded though, and I think it was never claimed that Random is thread safe, so I wouldnât blame it for being not thread safe.
What kind of âawfulâ did you find there?
This thread made me lol ;D
-Pickle
@Pickleninja: Threads that make you laugh are awesome.
@Varkas: It is thread safe, thatâs one of the problems! Seriously its main problem is that the design doesnât handle any use-case well. Be again, nothing to care about it youâre only singled threaded.
Iâm just curious to learn what it doesnât do well. I mostly use the nextInt() and nextLong() methods, and so far I couldnât notice anything bad from the random numbers that these produce. I googled and found a few warning abou the bad quality of java.util.Random but the pages that I saw didnât tell anything precise ⌠what exactly are the problems? Which use cases are handled badly?
Actually Iâm using ârng = new SecureRandom();â if available in my Solarex project since a while because it was said to be a better generator, but I couldnât figure much of a difference in my application.
Since I use RNGs a lot to create all kinds of things Iâm quite interested in learning about the problems of java.util.Random so I can make up my mind next time that I need a random number source.
Donât get me wrong, Iâm not trying to defend java.util.Random or so, Iâm just not aware of the problems.
Edit: Iâve googled some more, and it basically seems that it implements an older crng algorithm which was âstate of the artâ when the class was written, but there are better implementations known now, which give better random numbers at less CPU consumption.
Iâm not sure how this translated to âIt canât handle any use case wellâ, though. It should be good enough for most random number needs.
static final public int random()
{
//todo change this to 2
return 3;
}
Oh goody, another random thread. Rather than debate the finer points, letâs boil it down to some doâs and donâts:
- Donât use the static methods on Random. Create an instance of Random instead. If youâre saving a seed (e.g. for maps) then this advice isnât really optional.
- If you donât mind depending on java7, use ThreadLocalRandom.
- If you need a cryptographic random source, just stop and ask what youâre doing first. If youâre generating nonces for a web session, great, use SecureRandom. If youâre trying to actually encrypt something, then consider that if youâre expert enough to do crypto from scratch, you should be smart enough not to. Use a proper crypto implementation like BouncyCastle or Jasypt.