filmov
tv
Java | How to get random numbers (int/double/long/etc) 2020

Показать описание
In this tutorial I'll show you one way to get random numbers. So random integer, double, long etc. I hope you enjoy this tutorial about random numbers/how to make a random number generator and if you have any questions just leave them in the comments and I'll try to answere them :)
Text tutorial. (how to do it for strings is further down)
2: *Define a variable* to which you want to assign your random number to.
In the tutorial I showed it with an Integer but you can use doubles longs basically any data type that uses numbers.
3. Type this code. *'Your Variable' = new Random().nextInt('Range');*
You can replace the Int in next in if you used something other than an integer. So if you used double for example you would type "nextDouble"
Everything that I put under quotation ( ' ) needs to be replaced by you (without the quotation marks)
Now you are done.
*Code example: *
package main;
public class RNDM {
public static void main(String[] args) {
// TODO Auto-generated method stub
Double rdm;
rdm = new Random().nextDouble();
}
}
*How to do it for Strings using the "UUID method"*
2. Define a String.
3. Enter this code. 'Your Variable' = UUID.randomUUID().toString();
*Code example: *
public class JavaRandomStringExample {
public static void main(String[] args) {
for(int i=0; i"less than"10; i++){ //Youtube doesn't allow angled brackets
String strRandomString = UUID.randomUUID().toString();
}
}
}
I hope this helps :)
If you got any questions just leave them in the comments
Music:
Track 1: dj quads - it’s near
Text tutorial. (how to do it for strings is further down)
2: *Define a variable* to which you want to assign your random number to.
In the tutorial I showed it with an Integer but you can use doubles longs basically any data type that uses numbers.
3. Type this code. *'Your Variable' = new Random().nextInt('Range');*
You can replace the Int in next in if you used something other than an integer. So if you used double for example you would type "nextDouble"
Everything that I put under quotation ( ' ) needs to be replaced by you (without the quotation marks)
Now you are done.
*Code example: *
package main;
public class RNDM {
public static void main(String[] args) {
// TODO Auto-generated method stub
Double rdm;
rdm = new Random().nextDouble();
}
}
*How to do it for Strings using the "UUID method"*
2. Define a String.
3. Enter this code. 'Your Variable' = UUID.randomUUID().toString();
*Code example: *
public class JavaRandomStringExample {
public static void main(String[] args) {
for(int i=0; i"less than"10; i++){ //Youtube doesn't allow angled brackets
String strRandomString = UUID.randomUUID().toString();
}
}
}
I hope this helps :)
If you got any questions just leave them in the comments
Music:
Track 1: dj quads - it’s near
Комментарии