# Java Random numbers

## Java Random numbers

In Java, as well as any other programming language, you can generate a random number or sequence of random numbers. Here, it involves the use of one library of Java; the util library.

You need to import the library in order to use the random number feature:

`import java.util.Random;`

## Random Methods

Here is how to create a new Random object in Java:

`Random name = new Random();`

Where name is an appropriate name for the Random object. Once declared, you can begin to generate a random number by making use of an appropriate method. Here is a short list that you will make the most use of when dealing with random numbers:

## int nextInt()

This method will return a pseudorandom integer. This will cover all 232 possibilities of integers (both positive and negative).

## int nextInt(int range)

This method will return a pseudorandom integer in the range: 0 <= x < range. Notice that the range is not included and 0 is. So say that you wanted to go from 1 to 10. You need to avoid 0 in that mix and you need to include the topmost value range. This is a good way of doing that:

`int num = r.nextInt(10) + 1;`

So even if 9 is generated, it will add 1 and make it 10 and similarly with 0, it will make it 1.

## double nextDouble() float nextFloat()

This will generate numbers in the range 0.0 to 1.0 inclusive.

## long nextLong()

This will generate random numbers in the range 2^64 based on the long type.

## boolean nextBoolean()

This will generate either true or false respectively.

Here is an example program that will fill an array with random numbers:

## Example 1: Random Arrays

```import java.util.Random;
public class ArrayRandom{
public static void main(String args[]){
Random r = new Random();
int arr[] = new int;

for(int i = 0; i < 20; i++){
//random numbers from 1 to 10:
arr[i] = r.nextInt(10) + 1;
}

for(int i = 0; i < 20; i++){
System.out.print(arr[i] + " ");
}
} //main
} //class```

The program simply places random numbers from 1 to 10 inside the array called arr. It will output the values in the array in the second program. This is a simple program to see how random numbers work. Run the program multiple times to see the different numbers appear.

## Example 2: Random Array’s 2

```import java.util.Random;
public class ArrayRandom2{
public static void main(String args[]){
Random r = new Random();
int arr[] = new int;

for(int i = 0; i < 1000; i++){
//random numbers from 1 to 10:
arr[r.nextInt(25)] ++;
}

for(int i = 0; i < 25; i++){
System.out.println(i + " was generated " +
arr[i] + " times.");
}
} //main
} //class```

This program will keep track of how many times a random number was drawn. This will increment the appropriate place in the array. The generated number itself will be the index of the array. One possible output can be:

```0 was generated: 33 times.
1 was generated: 33 times.
2 was generated: 43 times.
3 was generated: 44 times.
4 was generated: 45 times.
5 was generated: 39 times.
6 was generated: 44 times.
7 was generated: 40 times.
8 was generated: 41 times.
9 was generated: 43 times.
10 was generated: 37 times.
11 was generated: 38 times.
12 was generated: 42 times.
13 was generated: 37 times.
14 was generated: 38 times.
15 was generated: 42 times.
16 was generated: 30 times.
17 was generated: 37 times.
18 was generated: 42 times.
19 was generated: 38 times.
20 was generated: 33 times.
21 was generated: 52 times.
22 was generated: 45 times.
23 was generated: 51 times.
24 was generated: 33 times.```

The output will be different each time the program is run.

## Example 3: Random Stars

```import java.util.Random;
public class RandomStars{
public static void main(String args[]){
Random r = new Random();
int num = 0;

for(int i = 0; i < 25; i++){
//random numbers from 0 to 15:
num = r.nextInt(16);
for(int j = 0; j < num; j++)
System.out.print("*");
System.out.println();
}
} //main
} //class```

The program will simply output rows of stars to the screen 25 times. However, you may not see 25 rows since the inner loop will strictly be less than the generated number. If the random number is 0, the loop will not run and leave a blank row.

```One possible output is this:

****
**

*
********
****
**
************
*******

***

*******
****

*********
*****
*****
***

********
*******
****
**

Another possible output is this:

*********
****
******
**
******
***
*********
********
********
****
***
************
**
*******
********
***
*************
*****
*********
***
******
**********
********
******
**```