Java Random Tutorial (Math.random() vs Random Class nextInt() nextDouble() )

preview_player
Показать описание
Random numbers are a common part of many programs and games. (Yeah I know they're not truly "random" but pesudorandom numbers are the best computer can do)
There are 2 main ways: Using a static method of the built-in Math class, or creating a Random object & calling methods on that object

-Not very useful on its own, but the result it can be multiplied to give a "range" of number
-The upper bound is always excluded so you'd need to add 1 to get an inclusive range

int min = 5;
int max = 15;
int range = max - min +1;

-Range must be 1 greater than the range you want since the upper bound will always be excluded
-This will give you the desired result
-Negative values for min & max still work as long as you still add 1 to the range
-Getting decimals just involves removing the (int) casting
-You might want to round off the numbers to only display a few decimals

➣Random class
-Random generator = new Random();
-Creates a new Random object with reference variable "generator"
-You can now use the formula like this:
(int)(generator .nextDouble()*range ) +min
-But you can also use get an integer in a specific range
-The formula can now be used like this

Рекомендации по теме
Комментарии
Автор

The nice fast explanations are excellent, no fucking around with shitty opening scenes and introductions. Thank for the clear and concise explanations

ohnoitsaj
Автор

To the point + explained it fast without making it harder to comprehend what's going on. Perfect.

thoughte
Автор

I paused at 1:12 to like this video . Great help. Thanks

srinivasnangunuri
Автор

That is a great tutorial
I apreciated the "League legends caster" voice speed

paolobena
Автор

omg yes finally a video that does not beat around the bush and actually gets to the point THANK YOU brilliant

sheonajoseph
Автор

How to generate exact next appearing 5 random numbers
Please answer Sirr

shreyas-sfeu
Автор

thank you for this video, this helps me so much for my crrent problem

MD-socz
Автор

Using Math.random on your example, can you generate an output sorted from lowest to highest?

mathiascronqvist
Автор

Hi, so what are the differences between the two methods in terms of pros and cons?

deanlgj
Автор

Hey bro nice vid. Do you know how to calculate random numbers?

FoodImitatingArt
Автор

what is better ? the Random class or the function random from the Math class?

XOPGAMINGX
Автор

great but if i want to get numbers without repeat and full from between 1-8 ? look like {6, 4, 1, 2, 8, 5, 7, 3} ?

hkena
Автор

Man your voice is so funny and amazing just slow it down a bit please.

wajahatghumman
Автор

What if i wanna use numbers between 5000 and

Supashotzz
Автор

I did something wrong, because I can't generate more than ONE number with a System.out.println.
I had to put 6 to generate 6 numbers.
Here's my code

package desafio;

import java.util.Random;

public class Megasena {
public static void main(String[] args) {

Random dezenas=new Random () ;
for (int i = 0; i<1; i++) {

}








}
}

DanielBelini
Автор

ok ok
i opened this video to hear that "Random class is 2 times faster than Math.random() method"
but you just explained both of them and nothing more, which is disappointing :|
anyway, nice video +

keithwallace
Автор

haha, you were probably not in hurry while making this video.

ali.darwish
Автор

Turn the speed to 0.5x. Thank me later.

jenniferlacaste
Автор

Stop gabbling - slow down and articulate.

elizabethhayes