0% found this document useful (0 votes)
37 views1 page

String: Public Class Public Static

This Java program demonstrates how to generate random numbers using the Math.random() method. It first generates 5 random numbers between 0 and 1, then generates 5 random integers between 1 and 100 by multiplying the Math.random() output by 100 and casting to an int. The typical output shows examples of the random numbers generated.

Uploaded by

Sahil Malhotra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views1 page

String: Public Class Public Static

This Java program demonstrates how to generate random numbers using the Math.random() method. It first generates 5 random numbers between 0 and 1, then generates 5 random integers between 1 and 100 by multiplying the Math.random() output by 100 and casting to an int. The typical output shows examples of the random numbers generated.

Uploaded by

Sahil Malhotra
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.

public class GenerateRandomNumbers {


public static void main(String[] args) {
/*
* To generate random numbers, use
* static double random() method of Java Math class.
*
* This method returns a positive double value grater than 0.0
* and less than 1.0
*/
System.out.println("Random numbers between 0.0 and 1.0 are,");
for(int i=0; i < 5 ; i++)
System.out.println("Random Number ["+ (i+1) + "] : " + Math.random());
/*
* To generate random number between 1 to 100 use following code
*/
System.out.println("Random numbers between 1 and 100 are,");
for(int i=0; i < 5 ; i++)
System.out.println("Random Number ["+ (i+1) + "] : " +(int)(Math.random()*100));
}
}
/*
Typical output would be
Random numbers between 0.0 and 1.0 are,
Random Number [1] : 0.7900395454653136
Random Number [2] : 0.15887365598103076
Random Number [3] : 0.5570570713930629
Random Number [4] : 0.017811004461356195
Random Number [5] : 0.7135560403213513
Random
Random
Random
Random
Random
Random
*/

numbers between 1 and 100 are,


Number [1] : 31
Number [2] : 21
Number [3] : 24
Number [4] : 95
Number [5] : 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy