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 44 45 46 47 48 49 |
/* Generate random numbers using Math.random This java example shows how to generate random numbers using random method of Java Math class. */ 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 numbers between 1 and 100 are, Random Number [1] : 31 Random Number [2] : 21 Random Number [3] : 24 Random Number [4] : 95 Random Number [5] : 3 */ |
Isn’t it Random numbers between 0 and 100?
i believe it is between 0 and 99.99 all inclusively
No, it is using type int meaning that it can only be integer numbers. No decimal places may be used.
jo tuc samz lo…hahahhahahah
Actually between 0 and 99. If they wanted 1 through 100 they would have to do (int)(Math.random()*100+1);
Please tell : methods to create array, add elements into it, shuffle contents of
array, search for an element in the array.
That works also, how ever is much more complex than the one I have suggested.
Aap sikho tan yaar…….
Is it possible to generate a number that when it generates another that is NOT the same one.
For example to avoid an output like this;
1
4
2
1
3
Is there a way to avoid the 1 appearing twice?
Use an if statement. If you get a number previously outputed then simply draw another number. Becareful that you have enough options or you will get an error/non-ending program.
I am a total newbie what programming
how should i start that????
thank you
variables: double a, b, c;
int d, e, f, g;
a <— random number in the interval [0; 1[
b <— random number in the interval [0; 3.141592[
c <— random number in the interval [4.08; 15[
d <— whole random number in the interval [23, 42[
e <— whole random number in the interval [6, 49[
f <— whole random number in the interval [0, 36[
g <— whole random number in the interval [1; 100[
Output: Values ??of variables a to g?
How would I use an if statement to find the highest value among the random numbers (1-100)?