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

Опубликовано: 18 Ноябрь 2020
на канале: Reewen
2,134
17

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 :)

My help Discord:   / discord  

Text tutorial. (how to do it for strings is further down)


1. Import the Random number utility. So add this at the beginning of your project: import java.util.Random;


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;

import java.util.Random;

import javax.swing.JOptionPane;

public class RNDM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Double rdm;

rdm = new Random().nextDouble();
JOptionPane.showMessageDialog(null, rdm);

}

}


*How to do it for Strings using the "UUID method"
1. Import this: import java.util.UUID;


2. Define a String.


3. Enter this code. 'Your Variable' = UUID.randomUUID().toString();


And you are done. This will only output in the typical "UUID format". If you need it to work differently here is a website with a lot of different ways on how to do it: https://www.javacodeexamples.com/crea...




*Code example: *
package com.javacodeexamples.stringexamples;

import java.util.UUID;

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();
System.out.println( strRandomString );

}

}

}



I hope this helps :)
If you got any questions just leave them in the comments



Music:
Track 1: dj quads - it’s near
YouTube link:    • dj quads - it’s near  


Смотрите видео Java | How to get random numbers (int/double/long/etc) 2020 онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Reewen 18 Ноябрь 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,134 раз и оно понравилось 17 людям.