Java udostępnia trzy sposoby generowania liczb losowych przy użyciu wbudowanych metod i klas wymienionych poniżej:
- klasa java.util.Random Metoda Math.random: Może generować liczby losowe typu podwójnego. Klasa ThreadLocalRandom
1) java.util.Random
- Aby użyć tej klasy do generowania liczb losowych, musimy najpierw utworzyć instancję tej klasy, a następnie wywołać metody takie jak nextInt(), nextDouble(), nextLong() itp., używając tej instancji.
- Za pomocą tej klasy możemy generować losowe liczby typu integer, float, double, long, boolean.
- Możemy przekazywać argumenty do metod wyznaczających górną granicę zakresu generowanych liczb. Na przykład nextInt(6) wygeneruje liczby z zakresu od 0 do 5 włącznie.
Jawa
// A Java program to demonstrate random number generation> // using java.util.Random;> import> java.util.Random;> > public> class> generateRandom{> > > public> static> void> main(String args[])> > {> > // create instance of Random class> > Random rand => new> Random();> > > // Generate random integers in range 0 to 999> > int> rand_int1 = rand.nextInt(> 1000> );> > int> rand_int2 = rand.nextInt(> 1000> );> > > // Print random integers> > System.out.println(> 'Random Integers: '> +rand_int1);> > System.out.println(> 'Random Integers: '> +rand_int2);> > > // Generate Random doubles> > double> rand_dub1 = rand.nextDouble();> > double> rand_dub2 = rand.nextDouble();> > > // Print random doubles> > System.out.println(> 'Random Doubles: '> +rand_dub1);> > System.out.println(> 'Random Doubles: '> +rand_dub2);> > }> }> |
>
>Wyjście
Random Integers: 618 Random Integers: 877 Random Doubles: 0.11981638980670772 Random Doubles: 0.7288425427367139>
2) Matematyka.losowość()
Klasa Math zawiera różne metody wykonywania różnych operacji numerycznych, takich jak obliczanie potęgowania, logarytmów itp. Jedną z tych metod jest random(), ta metoda zwraca podwójną wartość ze znakiem dodatnim, większą lub równą 0,0 i mniejszą niż 1,0 . Zwracane wartości są wybierane pseudolosowo. Ta metoda może generować tylko liczby losowe typu Doubles. Poniższy program wyjaśnia sposób użycia tej metody:
Jawa
// Java program to demonstrate working of> // Math.random() to generate random numbers> import> java.util.*;> > public> class> generateRandom> {> > public> static> void> main(String args[])> > {> > // Generating random doubles> > System.out.println(> 'Random doubles: '> + Math.random());> > System.out.println(> 'Random doubles: '> + Math.random());> > }> }> |
>
>Wyjście
Random doubles: 0.40748894116045375 Random doubles: 0.006683607229094002>
3) klasa java.util.concurrent.ThreadLocalRandom
Klasa ta została wprowadzona w Javie 1.7 w celu generowania liczb losowych typu całkowitego, podwójnego, logicznego itp. Poniżej program wyjaśnia, jak używać tej klasy do generowania liczb losowych:
Jawa
// Java program to demonstrate working of ThreadLocalRandom> // to generate random numbers.> import> java.util.concurrent.ThreadLocalRandom;> > public> class> generateRandom> {> > public> static> void> main(String args[])> > {> > // Generate random integers in range 0 to 999> > int> rand_int1 = ThreadLocalRandom.current().nextInt();> > int> rand_int2 = ThreadLocalRandom.current().nextInt();> > > // Print random integers> > System.out.println(> 'Random Integers: '> + rand_int1);> > System.out.println(> 'Random Integers: '> + rand_int2);> > > // Generate Random doubles> > double> rand_dub1 = ThreadLocalRandom.current().nextDouble();> > double> rand_dub2 = ThreadLocalRandom.current().nextDouble();> > > // Print random doubles> > System.out.println(> 'Random Doubles: '> + rand_dub1);> > System.out.println(> 'Random Doubles: '> + rand_dub2);> > > // Generate random booleans> > boolean> rand_bool1 = ThreadLocalRandom.current().nextBoolean();> > boolean> rand_bool2 = ThreadLocalRandom.current().nextBoolean();> > > // Print random Booleans> > System.out.println(> 'Random Booleans: '> + rand_bool1);> > System.out.println(> 'Random Booleans: '> + rand_bool2);> > }> }> |
>
>Wyjście
Random Integers: -2106603442 Random Integers: 1894823880 Random Doubles: 0.6161052280172054 Random Doubles: 0.8418944588752132 Random Booleans: false Random Booleans: true>
Aby wygenerować liczby losowe z określonymi zakresami. Można to zrobić na 2 różne sposoby:
- Korzystanie z losowej klasy
- Korzystanie z metody Math.random().
1. Korzystanie z losowej klasy
Oto wzór na wygenerowanie liczb losowych z określonego zakresu, gdzie min i max to nasza dolna i górna granica liczby.
Losowy rand = nowy Losowy();
int randomNum = rand.nextInt(max – min + 1) + min;
Jawa
import> java.io.*;> import> java.util.*;> class> GFG {> > public> static> void> main (String[] args) {> > Random rand => new> Random();> > int> max=> 100> ,min=> 50> ;> > System.out.println(> 'Generated numbers are within '> +min+> ' to '> +max);> > System.out.println(rand.nextInt(max - min +> 1> ) + min);> > System.out.println(rand.nextInt(max - min +> 1> ) + min);> > System.out.println(rand.nextInt(max - min +> 1> ) + min);> > }> }> |
>
>Wyjście
Generated numbers are within 50 to 100 58 87 55>
Złożoność czasowa: ma złożoność czasową O(1)
Przestrzeń pomocnicza: O(1) wymaga stałej przestrzeni.
2. Korzystanie z metody Math.random().
Oto wzór na wygenerowanie liczby losowej z określonego zakresu, gdzie min i max to nasza dolna i górna granica liczby:
int randomNum = min + (int)(Math.random() * ((max – min) + 1));
Jawa
tostrowanie Java
/*package whatever //do not write package name here */> import> java.io.*;> import> java.util.*;> class> GFG {> > public> static> void> main (String[] args) {> > int> max=> 100> ,min=> 50> ;> > System.out.println(> 'Generated numbers are within '> +min+> ' to '> +max);> > System.out.println(min + (> int> )(Math.random() * ((max - min) +> 1> )));> > System.out.println(min + (> int> )(Math.random() * ((max - min) +> 1> )));> > System.out.println(min + (> int> )(Math.random() * ((max - min) +> 1> )));> > }> }> |
>
>Wyjście
Generated numbers are within 50 to 100 53 99 77>
Złożoność czasowa: ma złożoność czasową O(1)
Przestrzeń pomocnicza: O(1) wymaga stałej przestrzeni.