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

Published: 18 November 2020
on channel: 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  


Watch video Java | How to get random numbers (int/double/long/etc) 2020 online without registration, duration hours minute second in high quality. This video was added by user Reewen 18 November 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 2,134 once and liked it 17 people.