Java Programming Tutorial – 26 – Random Number Generator

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

44 responses to “Java Programming Tutorial – 26 – Random Number Generator”

  1. Curtis Tackie Avatar

    Thanks this was helpful

  2. Bryant Blackham Avatar

    Great tutorial, thank you! Can you just put 7 in the dice.nextInt parameters dice.nextInt(7)?

  3. MrZombie1243 Avatar

    My first longer code. I hope I wrote it correct.

    import java.util.Random;
    import java.util.Scanner;

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

    Scanner input = new Scanner(System.in);
    Random dice = new Random();

    int tokens = 100;

    System.out.println("Your curent status is 100 tokens");
    System.out.println("If you get an even number from 2 to 20 you will win 10tokens, otherwise you lose 20tokens.nIf you want to role the dice press 0");

    int korisnik = input.nextInt();

    while(korisnik == 0) {

    int number = 2 + dice.nextInt(20);
    System.out.println("Your number is " + number);

    if(number % 2 == 0) {

    tokens += 10;
    System.out.println("Congradulation, you just won 10 tokens.nYou now have " + tokens + " tokens.nPress 0 again if you want to play more!");
    korisnik = input.nextInt();

    }
    else {

    tokens -=20;
    System.out.println("Sorry you just lost 20 tokens.n You now have " + tokens + " tokens.nPress 0 again if you want to play more!");
    korisnik = input.nextInt();
    }

    if(tokens <= 0) {
    System.out.println("Sorry, you ran out of tokens.");
    break;
    }

    }

    input.close();
    }

    }

  4. lfrg04 Avatar

    What if I need to generate a random number between say 50 to 100?

  5. Jay Rt Avatar

    try my slot machine

    package random;
    import java.util.Random;
    import java.util.Scanner;

    public class RNG {

    public static void main(String args[]){
    Random jackpot = new Random();
    Scanner input = new Scanner(System.in);
    int spin1,spin2,spin3,user_ans;

    System.out.println("Welcome to slot machine!!!");
    System.out.println("Would you like to test your luck today?");
    System.out.println("If you wish to continue press 1");
    user_ans = input.nextInt();

    while(user_ans==1){
    user_ans = 0;
    spin1 = 1+jackpot.nextInt(7);
    spin2 = 1+jackpot.nextInt(7);
    spin3 = 1+jackpot.nextInt(7);

    System.out.println(spin1+" "+spin2+" "+spin3);

    if(spin1==spin2 && spin1==spin3){
    System.out.println("Congrats!!! You have hit the jackpot");
    }
    else{
    System.out.println("Sorry! you lost this round");
    }
    System.out.println("Do you wish to give one more try?");
    System.out.println("If you wish to continue press 1");
    user_ans = input.nextInt();

    }

    System.out.println("Thank you for trying out our system");
    System.out.println("Please visit again");
    }
    }

  6. Mekeninzo Avatar

    I hate everyone using random() for random generators. You are polluting google with garbage randomizers and making it super hard to find information on real random number generators.

    You did not build a random number generator, you are just using the built in one from java you retard.

  7. Mazen Abraham Avatar

    does anyone has an idea how the computer generates random numbers ?

  8. HATIM DALAL Avatar

    In line number 9 can't we just do 'dice.nextInt(6+1);' instead of '1+dice.nextInt(6);' ?

  9. Redstone Flash Avatar

    Roll a die* grammar nazi here

  10. Arjun Unadkat Avatar

    I made a small slot machine game using what I have learnt so far from these tutorials:

    import java.util.Random;
    import java.util.Scanner;
    public class casino {

    public static void main(String[] args) {
    Random rnum = new Random();
    Scanner s = new Scanner(System.in);
    int rvalue;

    for(int counter = 0;counter<=10;counter++){
    System.out.print("Press Enter to play");
    s.nextLine();
    rvalue = 1+rnum.nextInt(10);

    switch(rvalue){

    case 3:
    System.out.println(rvalue);
    System.out.println("You got the lucky number 3! You won $1.00!");
    break;

    case 7:
    System.out.println(rvalue);
    System.out.println("You got the lucky number 7! You won $5.00!");
    break;

    case 10:
    System.out.println(rvalue);
    System.out.println("You got the lucky number 10! You won the jackpot prize $20.00!");
    System.exit(0);
    break;

    default:
    System.out.println(rvalue);
    break;
    }
    }
    }
    }

  11. Maurizio Girioli Avatar

    so, i had a little trouble but at last i found out where is was wrong, now i just need to clarify a thing

    why doing this rolls the dice the 10 times

    for(int counter=1; counter<=10;counter++){
    number = 1+dice.nextInt(6);
    System.out.println(number + " ");
    }

    but this rolls it only 1 time?

    for(int counter=1; counter<=10;counter++);{ // look the semi coma after the loop
    number = 1+dice.nextInt(6);
    System.out.println(number + " ");
    }

    i know its a dumb question, but that is why i am asking it, to stop being so dumb lol

  12. Zzzzzz... Avatar

    I created a rock paper sizzors game. The computer is well a little over confident…

    import java.util.Scanner;
    import java.util.Random;
    public class TheGame {
    public static void main(String args[]){
    int player_score=0;
    int computer_score=0;
    int counter=0;
    Random random=new Random();
    Scanner user_input=new Scanner(System.in);
    System.out.println("Welcome to Rock, paper, sizzors!");
    System.out.println("First one to score 10 points wins.");
    System.out.println("Can you beat the current grand champion, the computer?");
    System.out.println("To pick Rock, enter r, to pick paper, enter p, to pick sizzors, enter s.");
    System.out.println("Good luck, you will need it.");
    System.out.print("Enter your name here: ");
    String player_name=user_input.nextLine();
    System.out.println("You will now meet your doom " + player_name+"!");
    System.out.println("Mwahahaha!");
    while (counter==0){
    System.out.println("Enter 1 for rock, 2 for paper, 3 for sizzors.");
    int player_choice=user_input.nextInt();
    if ((player_choice==0) | (player_choice>3)){
    System.out.println("Please enter a valid number, rock=1, paper=2, sizzors=3.");
    int player_new=user_input.nextInt();
    player_choice=player_new;
    }
    int computer_choice=1+random.nextInt(3);

    if ((player_choice==1)&(computer_choice==1)){
    System.out.println("I choose rock.");
    System.out.println("Tie!");
    System.out.println("Lucky! Next round you won't be so lucky!");
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==1)&(computer_choice==2)){
    System.out.println("I choose paper.");
    System.out.println("I won this round! Hahaha!");
    computer_score++;
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==1)&(computer_choice==3)){
    System.out.println("I choose sizzors.");
    System.out.println(player_name + " won this round! Not bad for a petty human!");
    player_score++;
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==2)&(computer_choice==1)){
    System.out.println("I choose rock.");
    System.out.println(player_name + " won this round! I will beat you next round human, you will see!");
    player_score++;
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==2)&(computer_choice==2)){
    System.out.println("I choose paper.");
    System.out.println("Tie! Next time there won't be a tie as I will claim victory!");
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==2)&(computer_choice==3)){
    System.out.println("I choose sizzors.");
    System.out.println("Ha! You have lost this round puny human!");
    computer_score++;
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==3)&(computer_choice==1)){
    System.out.println("I choose rock.");
    System.out.println("Ha! I won this round. Give up now pathetic human!");
    computer_score++;
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==3)&(computer_choice==2)){
    System.out.println("The computer choose paper.");
    System.out.println(player_name + " won this round!");
    System.out.println("You may have won this round, but you havn't won the war! You will see.");
    player_score++;
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if ((player_choice==3)&(computer_choice==3)){
    System.out.println("I choose sizzors.");
    System.out.println("Tie! What?! A tie? Interesting…");
    System.out.println("Computer Score: " + computer_score);
    System.out.println(player_name + ": " + player_score);
    }
    if (player_score==10){
    System.out.println(player_name + " wins!");
    System.out.println("What?!?!?! Impossible! This is an outrage! You must have cheated! Farewell you lucky human.");
    counter++;
    }
    if (computer_score==10){
    System.out.println("I win! Hahahahahahahahahaha!");
    System.out.println("I told you I would win puny human! Machines claim triumph on this day!");
    System.out.println("I will now go rule the world!");
    counter++;
    }
    }
    }

    }

  13. Ferovax Avatar

    What if i wanted to generate a random number that is n digits long (n being a parameter passed)

  14. RPGPlayZ Avatar

    what's the ide you use

  15. XOPGAMINGX _300 Avatar

    how does it work??(the Random class) im just curious and i want to fully understand it (i already knew about the Random class and i just wanted to know how it works)

  16. Abhinav Yadav Avatar

    Simple Roulette Casino game:

    import java.util.Random;
    import java.util.Scanner;

    public class Roulette {
    public static void main(String[] args) {
    Random roll = new Random();
    Scanner guess = new Scanner(System.in);
    int input, play;
    int option = 0;

    do {
    System.out.print("Enter your number from 1 to 6: ");
    input = guess.nextInt();
    System.out.print("The wheel says: ");
    play = 1 + roll.nextInt(6);
    System.out.println(play);
    if (input == play) {
    System.out.println("nCongratulations!!nYou have won $1 million jackpot!n");
    } else {
    System.out.println("nSorry. Hard luck!nnDo you want to play again?n Press 1 for Yesn Press 0 for No");
    option = guess.nextInt();
    }
    } while (option == 1 && input != play);

    }

    }

  17. Alexandru Ionita Avatar

    Why does "nextInt()" take values from 0 to … and not also negative values?

  18. AmazingCraft0210 Avatar

    My dice roll program

    import java.util.Scanner;
    import java.util.Random;
    public class Dice { public static void main(String[] args) {
      Scanner keyboard = new Scanner(System.in);
      Random myRandom = new Random();
      
      int dice;
      int random;
      
      for(int counter=0;counter<=10;counter++){
       random = myRandom.nextInt(6) + 1;
       
       if(random == 1){
        System.out.println("You rolled a 1!");
        
       }else if(random == 2){
        System.out.println("You rolled a 2!");
        
       }else if(random == 3){
        System.out.println("You rolled a 3!");
        
       }else if(random == 4){
        System.out.println("You rolled a 4!");
        
       }else if(random == 5){
        System.out.println("You rolled a 5!");
        
       }else if(random == 6){
        System.out.println("You rolled a 6!");
         
        }
       }
      }
      }

  19. AmazingCraft0210 Avatar

    Another Coin Tosser

    import java.util.Scanner;
    import java.util.Random;
    public class CoinTossser { public static void main(String[] args) {
      Scanner keyboard = new Scanner(System.in);
      Random myRandom = new Random();
      int coin;
      
      
      for(int counter=0;counter<=10;counter++){
       coin = myRandom.nextInt(2) + 1; 
      
      
      
      if (coin == 1){
       System.out.println("It is heads");
       
      }else if(coin == 2){
       System.out.println("It is tails");
      }
       }}
     
    }

  20. paolo bergomi Avatar

    Thanks a lot again!

  21. Esam Elwan Avatar

    why can't you just increment on the value in the parantheses instead of adding a plus one to the variable number?

  22. Blade Mercy Avatar

    why not just make the variable have a stored value, "int number = 0" instead of "1+dice.next(6)" ?
    3:39

  23. Gurman Gill Avatar

    You can use directly int number = 1 + (int) (Math.random() * 6); // pretty simple and no need to include java.util.Random

  24. Fajol Miah Avatar

    Hi
    Thanks for uploading these tutorials.

    How do I empty an integer variable? I want to do it by clicking the button. Thanks

  25. CallMeNAZ Avatar

    I want random numbers to generate from -100 to +100. I tried this method it didn't work. Someone please help me. Any help will be appreciated

  26. MiauMiau :3 Avatar

    You could have done this:
    System.out.println((1 + dice.nextInt(6)).toString());

    You wouldn't need "number" this way. Good tutorial though

  27. Emmanuel Awona Avatar

    public static void main(String[] args) {
    int i;
    System.out.println(factoriel(5));
    System.out.println(factoriel(0));
    System.out.println(factoriel(-4));
    }

    public static int factoriel(int number) {
    int i = number;

    if (i == 0 || i == 1) {
    return 1;
    } else if (i < 0) {
    return -1;

    } else {
    return i * (i – 1);
    }

    }

    }

  28. ananda wagle Avatar

    what is the meaning of this?

    String name = names[(int) (Math.random() * names.length)];

  29. Arian D Avatar

    You can make it look like a dice

    package dice;

    import java.util.Random;
    import java.util.Scanner;

    public class dice {

    public static void main(String[] args) {
    Random dice = new Random();
    Scanner in = new Scanner(System.in);
    int number;
    System.out.print("How man times do you want to roll the dice? ");
    int times = in.nextInt();

    for (int i =0; i < times; i++){
    number = 1+dice.nextInt(6);
    //If you don't put +1 it's going to show numbers from 0 t 5
    System.out.println("-");
    switch (number) {
    case 1:
    System.out.println("| |n| 0 |n| |");
    break;
    case 2:
    System.out.println("| 0|n| |n|0 |");
    break;
    case 3:
    System.out.println("|0 |n| 0 |n| 0|");
    break;
    case 4:
    System.out.println("|0 0|n| |n|0 0|");
    break;
    case 5:
    System.out.println("|0 0|n| 0 |n|0 0|");
    break;
    case 6:
    System.out.println("|0 0|n|0 0|n|0 0|");
    break;
    }
    System.out.println("-");
    }
    }

    }

  30. MikiPiki Avatar

    I just use Math.random() * 6. Its much more simple

  31. Max Jacobson Avatar

    I saw where this was going and tried to complete it without watching the rest of the video. My code a bit overkill, but it's another way of doing this same thing.

    package bucky;

    import java.util.Random;

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

    Random dice = new Random();
    int number;

    for(int i=1; i <= 10; i++ ) {
    number = dice.nextInt(7);
    if(number == 0) {
    i–;
    continue;
    }
    System.out.println(number);
    }

    }
    }

  32. pupu41784 Avatar

    System.out.println(number + " ");
    Why put " " ? Worked fine without it.

  33. Paul Lewis Avatar

    I'm curious, is it possible to do this alpha-numerically with symbols? for instance, 1, 1b, 1#, 2b, 2# 2… etc. I'm thinking for musical scale note position memorization. Cheers,

  34. RokasDesigns Avatar

    I made a generator where you can set the range of min-max number to generate in. Tell me if I am just stupid and missed something extremely simple, and made it overly confusing.

    import java.util.Random;
    import java.util.Scanner;

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

    Scanner input = new Scanner(System.in);
    Random rnd = new Random();
    int number;

    System.out.println("Min: ");
    int minimum = input.nextInt();

    System.out.println("Max: ");
    int maximum = input.nextInt();

    number = 1 + rnd.nextInt(maximum);

    if(number >= minimum && number <= maximum){
    System.out.println(number);
    }else if(number + minimum <= maximum){
    System.out.println(number + minimum);
    }else if(maximum – number <= minimum){
    System.out.println(number + minimum);
    }else{
    System.out.println("I dunno wtf is wrong xD");
    }

    }

    }

  35. Ahmad Habib Avatar

    what does random number have to do with java..??
    is there any main use..?

  36. ArSid Meta Avatar

    Hey guys I made a simple Roulette Game where you start off with 10k with the goal 100k to pay off your debts:
    http://pastebin.com/gcmWyPfS

  37. sathursan ushakaran Avatar

    whats the dice for in "1+dice.nextint(6)"

  38. Rishav Chakraborty Avatar

    can you tell me how to do the coprime no.s

  39. Kisagi Seirei Avatar

    Finally the video I've been waiting to see since the beginning of this tutorial series. Great to know that there is a method we can just use instead of creating our own RNG. Thanks!

  40. Robert Alm Avatar

    I noticed that while we need to import library for random, we don't need to import any library for math. does anyone care to explain what is going on with that? 🙂
    Thank you.

  41. Rohit_Veer Avatar

    I created a Coin Prediction Game
    Play this Game and Get Addicted!!

    package beta;

    import java.util.Random;
    import java.util.Scanner;

    public class delta {
    public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    Random coin = new Random();
    int decide;
    System.out.println("Welcome to coin Tossing Game nChoose you're Predictionn1. Headsn2. Tails");
    int prediction = input.nextInt();
    System.out.println("To Toss the Coin Kindly Press 0");
    int run = input.nextInt();
    if(run == 0){
    decide = 1+coin.nextInt(2);
    if(decide == 1 && prediction == 1){
    System.out.println("It's Heads nHurrreeen **You Won**");
    }else if (decide == 1 && prediction == 2){
    System.out.println("It's Heads nBad Luckn **You Loss**");
    }
    else if(decide == 2 && prediction == 1){
    System.out.println("It's Tails nBad Luckn **You Loss**");
    }else if (decide == 2 && prediction == 2){
    System.out.println("It's Tails nHurrreeen **You Won**");
    }
    }input.close();
    }
    }

  42. Rohit_Veer Avatar

    I created a Coin Prediction Game
    Play this Game and Get Addicted!!

    package beta;

    import java.util.Random;
    import java.util.Scanner;

    public class delta {
    public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    Random coin = new Random();
    int decide;
    int prediction;
    System.out.println("Welcome to coin Tossing Game nChoose you're Predictionn1. Headsn2. Tails");
    prediction = input.nextInt();
    System.out.println("To Toss the Coin Kindly Press 0");
    int run = input.nextInt();
    if(run == 0){
    decide = 1+coin.nextInt(2);
    if(decide == 1 && prediction == 1){
    System.out.println("It's Heads nHurrreeen **You Won**");
    }else if (decide == 1 && prediction == 2){
    System.out.println("It's Heads nBad Luckn **You Loss**");
    }
    else if(decide == 2 && prediction == 1){
    System.out.println("It's Tails nBad Luckn **You Loss**");
    }else if (decide == 2 && prediction == 2){
    System.out.println("It's Tails nHurrreeen **You Won**");
    }
    }input.close();
    }
    }

  43. Hotboytrue Avatar

    So couldnt i just put 7 instead of 6 for 1-6 and if i wanted 1-7 couldnt i just put 8?

Leave a Reply

Your email address will not be published. Required fields are marked *