Random Number?

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) :stuck_out_tongue:

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! :slight_smile: 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.