# Java Programming Tutorial – 26 – Random Number Generator

///Java Programming Tutorial – 26 – Random Number Generator

## Java Programming Tutorial – 26 – Random Number Generator

GitHub –
reddit –
Support –
thenewboston –

source

By |2020-09-08T09:18:42+00:00September 8th, 2020|Java Video Tutorials|48 Comments

1. Drew Bigelow September 8, 2020 at 9:18 am - Reply

2. ANJUM PARVEEN September 8, 2020 at 9:18 am - Reply
3. 005y GAMING September 8, 2020 at 9:18 am - Reply

Where are you coding?

4. Alibek Abdullaev September 8, 2020 at 9:18 am - Reply

Best guy I've ever known

5. clerp September 8, 2020 at 9:18 am - Reply

Don't you have to initialize the number variable first?

6. clerp September 8, 2020 at 9:18 am - Reply

1. I had an idea about how the RNG would work in this tutorial based on the previous videos in this series, and I couldn't wait to try it out before watching this video, so I tried writing my own code based on what bucky has gone over so far, and this is what I ended up with.
2. The best part is: it actually works, which is why I'm sharing this!
3. This will also generate a random integer between 0 and 100:
(int)(Math.ceil(Math.random()*100));
The only part of that I didn't get from what you've shown so far is the (int) part at the beginning. If you don't include the (int), it will work, but it will give you a decimal number in the tenths place. Also, if you use this code, you don't have to import anything.

7. Al Rashed Caris September 8, 2020 at 9:18 am - Reply

Thank you for this bro

Sorry I am beginner so why do we need the empty qoutation on the last line

9. Preeti Bajpai September 8, 2020 at 9:18 am - Reply

Isn't there any other way to generate a random number between 1 to 43

10. oud master September 8, 2020 at 9:18 am - Reply

thank you,
now that 1+ let me lose around 5 hours, I was trying to make a loop if Random elected 0, then repeat the process until it elects non 0.
Why would Java does not allow us to create ranges easily?
I want to create a Random number from specific range like 201 to 300!!
so should I always add values to the variables to achieve this?

11. charly mosby September 8, 2020 at 9:18 am - Reply

bucky you are great man, without ur videos i'll have not pass my exam this year. You condense 3 months of class in like 3 hour. <3

12. Truman Adrian Lobato De Faria September 8, 2020 at 9:18 am - Reply

Thank you so much for your tutorials, really easy to understand. I hope your smoker's cough or something is alright. <3

13. Carly McCalla September 8, 2020 at 9:18 am - Reply

This is ace thanks and your accent is cool!

14. nahian reza September 8, 2020 at 9:18 am - Reply

Where's the Indian accent?

Hi, Can someone tell me how to get code of Math.random() method. i.e., what is the code written inside this method. 🙏🙏🙏🙏

Bucky pronouce dice like : DOICE xD

Good

18. The Meme Council September 8, 2020 at 9:18 am - Reply

I just created a "Diavolo VS Giorno" fight using the Random, the Scanner, if statements and a lot of integers. It turned out so well and to be honest I actually had fun playing it, I guess I was intense when Diavolo was at 1500 HP and Giorno was at 13.

19. RideQuebec September 8, 2020 at 9:18 am - Reply

// Random dice = new Random();

// int number,number1,number2,number3,number4,number5,number6,number7,number8;

//

// for(int counter = 1; counter < 2; counter++) {

// number = 1 + dice.nextInt(9999);

// number1 = 1 + dice.nextInt(9999);

// number2 = 1 + dice.nextInt(9999);

// number3 = 1 + dice.nextInt(9999);

// number4 = 1 + dice.nextInt(9999);

// number5 = 1 + dice.nextInt(9999);

// number6 = 1 + dice.nextInt(9999);

// number7 = 1 + dice.nextInt(9999);

// number8 = 1 + dice.nextInt(9999);

//

// String firstPrize = String.format("%04d", number).substring(0, 4);

// String secondPrize = String.format("%04d", number1).substring(0, 4);

// String thirdPrize = String.format("%04d", number2).substring(0, 4);

// String fourthPrize = String.format("%04d", number3).substring(0, 4);

// String fifthPrize = String.format("%04d", number4).substring(0, 4);

// String sixthPrize = String.format("%04d", number5).substring(0, 4);

// String seventhPrize = String.format("%04d", number6).substring(0, 4);

// String eighthPrize = String.format("%04d", number7).substring(0, 4);

// String ninthPrize = String.format("%04d", number8).substring(0, 4);

//

// System.out.println("This Week's Draw");

// System.out.println();

// System.out.println("4D 1st Prize: " + firstPrize +" "+" "+" "+" "+" "+" "+" "+" "+" "+"||" + " Prize Money : \$2,000,000");

// System.out.println("4D 2nd Prize: " + secondPrize +" "+" "+" "+" "+" "+" "+" "+" "+" "+"||" + " Prize Money : \$500,000");

// System.out.println("4D 3rd Prize: " + thirdPrize +" "+" "+" "+" "+" "+" "+" "+" "+" "+"||" + " Prize Money : \$250,000");

// System.out.println("4D Starter Prize: " + fourthPrize +" "+" "+" "+" "+" "+"||" + " Prize Money : \$20,000");

// System.out.println("4D Starter Prize: " + fifthPrize +" "+" "+" "+" "+" "+"||" + " Prize Money : \$20,000");

// System.out.println("4D Starter Prize: " + sixthPrize +" "+" "+" "+" "+" "+"||" + " Prize Money : \$20,000");

// System.out.println("4D Consolation Prize: " + seventhPrize +" "+"||" + " Prize Money : \$10,000");

// System.out.println("4D Consolation Prize: " + eighthPrize +" "+ "||" + " Prize Money : \$10,000");

// System.out.println("4D Consolation Prize: " + ninthPrize +" "+"||" + " Prize Money : \$10,000");

// }

Subtitles also needed to understand your pronunciation because different types of people have to watch your videos

21. KOBA TUBE September 8, 2020 at 9:18 am - Reply

what if u wanna generate negative numbers

22. Wind 9993 September 8, 2020 at 9:18 am - Reply

What if I need to generate a random number from 1000 to 9999?

23. Alec Grakay September 8, 2020 at 9:18 am - Reply

Where does he get "counter" from? I am using eclipse and it tells me that doesn't work here. I haven't had the time to watch his previous videos if he explains earlier that would be nice to know too.

24. Sibi Stephen September 8, 2020 at 9:18 am - Reply

How do you find the Nth smallest number in the randomly generated 500 numbers b/w 100-1000? I have both the codes below.

Is there a way to merge both codes into one code so, to get both results under one console?

//Generate 500 RANDOM numbers

import java.util.Random;
public class RandomNumbers {

public static void main(String[] args) {
int counter;
Random rnum = new Random();
/* Below code would generate 500 random numbers
* between 100 and 1000.
*/
System.out.println("Random Numbers:");
System.out.println("***************");
for (counter = 100; counter <= 500; counter++) {
System.out.println(rnum.nextInt(1000));
}
}
}

*********************************************************

//Find the Nth smallest number (range from 100 – 1000)

public class Nthsmall {

public static void main(String[] args) {
int numbers [] = {10, 15, 20, -88};

int largest = numbers[0];
int smallest = numbers[0];

for(int i = 1; i<numbers.length; i++) {
if(numbers[i]>largest) {
largest = numbers[i];
}
else if(numbers[i]<smallest) {
smallest = numbers[i];
}
}
System.out.println("List of array is: " + Arrays.toString(numbers));
System.out.println("Largest numbers is : " +largest);
System.out.println("Smallest number is : " +smallest);
}
}

25. Stephen Profullo Bala September 8, 2020 at 9:18 am - Reply

Thank u so much! my lab xm is tomorrow and ur videos saved me

26. Fedi Bessrour September 8, 2020 at 9:18 am - Reply

"Since all computer counting starts at 0"

Matlab users lose their shit

27. Nail Bin September 8, 2020 at 9:18 am - Reply

who woulda guessed it! its me watching @2019!

i was looking for the EXACT This Lecture for MY TEST!

29. KZ beats September 8, 2020 at 9:18 am - Reply

thanks bro you just help me

30. Taleel September 8, 2020 at 9:18 am - Reply

I made a Rock Paper Scissors Game:

import java.util.Scanner;

import java.util.Random;

class Main {

public static void main(String[] args) {

Random computer= new Random();

Scanner option = new Scanner(System.in);

System.out.println("================================");

System.out.println("Welcome to Rock, Paper, Scissors");

System.out.println("================================");

System.out.println("Options-");

System.out.println("1-Rock");

System.out.println("2-Paper");

System.out.println("3-Scissors");

System.out.println("================================");

System.out.print("Select an option:");

int play = option.nextInt();

System.out.println("================================");

if ((play==0)||(play>3)){

System.out.println("Error:: Message:"+" Boi You better get that outta here");

}

int ai=1+computer.nextInt(3);

if ((play==1)&&(ai==1)){

System.out.println("Player Chose Rock");

System.out.println("Computer Chose Rock");

System.out.println("A Tie");

}else if ((play==1)&&(ai==2)){

System.out.println("Player Chose Rock");

System.out.println("Computer Chose Paper");

System.out.print("Computer Wins");

}else if ((play==1)&&(ai==3)){

System.out.println("Player Chose Rock");

System.out.println("Computer Chose Scissors");

System.out.print("Player Wins");

}else if ((play==2)&&(ai==1)){

System.out.println("Player Chose Paper");

System.out.println("Computer Chose Rock");

System.out.print("Player Wins");

}else if ((play==2)&&(ai==2)){

System.out.println("Player Chose Paper");

System.out.println("Computer Chose Paper");

System.out.print("A Tie");

}else if ((play==2)&&(ai==3)){

System.out.println("Player Chose Paper");

System.out.println("Computer Chose Scissors");

System.out.print("Computer Wins");

}else if ((play==3)&&(ai==1)){

System.out.println("Player Chose Scissors");

System.out.println("Computer Chose Rock");

System.out.print("Computer Wins");

}else if ((play==3)&&(ai==2)){

System.out.println("Player Chose Scissors");

System.out.println("Computer Chose Paper");

System.out.print("Player Wins");

}else if ((play==3)&&(ai==3)){

System.out.println("Player Chose Scissors");

System.out.println("Computer Chose Scissors");

System.out.print("A Tie");

}

}

}

31. Nitesh Kuriyal September 8, 2020 at 9:18 am - Reply

Food

32. Brian Dacallos September 8, 2020 at 9:18 am - Reply

2019 still watching this Legend !

33. hans September 8, 2020 at 9:18 am - Reply

how to generate a random number from a list?

34. Amish brotherhood September 8, 2020 at 9:18 am - Reply

This guy be recording from the amazon rainforest yet he's still the best java tutorial guy, love your videos bucky!

35. ƗdǾ ƕǞ DǬĸȄȐ ŊǬDeŖ September 8, 2020 at 9:18 am - Reply

stop snitching math ranndom

36. Derek September 8, 2020 at 9:18 am - Reply

i think this is the 5th time i watch this video, every month i forget how random works….
sooo see ya next month

37. Shreya Umesh September 8, 2020 at 9:18 am - Reply

rather than adding 1, we can just write: dice.nextInt(7); right?

how can i do this with a range of number like 3-6 ?

39. Krishore September 8, 2020 at 9:18 am - Reply

When using the Random class, does it equally divide the probability based on how many options you give it? So does dice.nextInt(6) mean each number has a 1/6 probability of occurring?

40. Imane Bouhsine September 8, 2020 at 9:18 am - Reply

thank you so much, but i did not understand nextInt, can u explain it to me please

41. Sabay Merl Videos September 8, 2020 at 9:18 am - Reply

Could you tell me. How to make group presentation by Random Number

42. 82 Kedar Wakade September 8, 2020 at 9:18 am - Reply

can i get these random numbers in a sequence,say if i can get these in ascending order

43. BigBaby2002 September 8, 2020 at 9:18 am - Reply

Who can help with this. There is a drunkard in a grid of streets randomly picks one of four directions and stumbles to the next interaction, and he does the same. If this drunkard walks 100 intersections, where would he be, starting at (0, 0). You are selected to create and design a program to solve the Drunkard's Walk problem.

44. Suji RJ September 8, 2020 at 9:18 am - Reply

i dont understand how the random numbers generated . Can anyone of you please explain the concept

45. Vibhas Wahi September 8, 2020 at 9:18 am - Reply

Why did this guy stop making videos??

46. Harjeck September 8, 2020 at 9:18 am - Reply

And what if I want the numbers to not repeat themselves.. ?.. I need answer.. thx. 🙂

47. Tdanso September 8, 2020 at 9:18 am - Reply

That's my man Bucky lol.

48. Seem's September 8, 2020 at 9:18 am - Reply