Java – Random Number Generator / How To Generate A Random Number

This is a simple method for generating a random number between zero (0) and a given number.

The following code example with generate a random number between 0 and 49.

Although the value of maxNumber is set to 50, Java will start the count at 0.

If you want a number between 0 and 50, set the value of maxNymber to 51.

int maxNumber = 50;
int randomNumber = (int)Math.floor(Math.random() * maxNumber);


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s