Generating Randoms but a few minor errors












2















I did look at the threads pertaining to randoms and implemented them into this assignment but I have 2 questions.



1) I need my program to generate random numbers (and print them) and count the iterations. I have counting the iterations down but I don't know why the random numbers don't print out. Does it have something to do with my guess = 0? Here's an example if I'm not clear.



Example: 
Enter a number: 13
85
89
73
94
13
This took 5 tries


2) I have no clue why my program always ends up stuck at one number for the answer. The program immediately ends after entering the number 86.



import java.util.*; 

public class FeelingLucky {
public static void main (String args) {
Scanner sc = new Scanner (System.in);

int tries = 0;
int guess = 0;

Random random = new Random(1);
int num = random.nextInt(100) + 1;

System.out.print("Pick a number between 1 and 100:");

while (guess != num) {
guess = sc.nextInt();
tries++;
}
System.out.println("It took " + tries + " tries to match");
sc.close();
}
}









share|improve this question

























  • Since you seed your Random with (1), you always get the same sequence of numbers. You need to print out the numers to print them. ie System.out.println("the random number is: " + num);

    – matt
    Nov 21 '18 at 14:15













  • your description is confusing, is the program guessing the number entered by user or is the user guessing the number generated by the program?

    – Milo Bem
    Nov 21 '18 at 14:23











  • @matt It just ends up with the same result even if I print them.

    – Marcos Z.
    Nov 21 '18 at 14:28











  • if the user is supposed to guess the number than your program works correctly. The reason it always "stops" at 86 is because 86 is the number generated. You guess the number - the program ends. Why it always generates this number is expained in the other comments and answers

    – Milo Bem
    Nov 21 '18 at 14:28











  • @MiloBem Oh my bad. The user is supposed to be guessing the number generated by the program and it prints out a bunch of random numbers till it reaches the result.

    – Marcos Z.
    Nov 21 '18 at 14:29
















2















I did look at the threads pertaining to randoms and implemented them into this assignment but I have 2 questions.



1) I need my program to generate random numbers (and print them) and count the iterations. I have counting the iterations down but I don't know why the random numbers don't print out. Does it have something to do with my guess = 0? Here's an example if I'm not clear.



Example: 
Enter a number: 13
85
89
73
94
13
This took 5 tries


2) I have no clue why my program always ends up stuck at one number for the answer. The program immediately ends after entering the number 86.



import java.util.*; 

public class FeelingLucky {
public static void main (String args) {
Scanner sc = new Scanner (System.in);

int tries = 0;
int guess = 0;

Random random = new Random(1);
int num = random.nextInt(100) + 1;

System.out.print("Pick a number between 1 and 100:");

while (guess != num) {
guess = sc.nextInt();
tries++;
}
System.out.println("It took " + tries + " tries to match");
sc.close();
}
}









share|improve this question

























  • Since you seed your Random with (1), you always get the same sequence of numbers. You need to print out the numers to print them. ie System.out.println("the random number is: " + num);

    – matt
    Nov 21 '18 at 14:15













  • your description is confusing, is the program guessing the number entered by user or is the user guessing the number generated by the program?

    – Milo Bem
    Nov 21 '18 at 14:23











  • @matt It just ends up with the same result even if I print them.

    – Marcos Z.
    Nov 21 '18 at 14:28











  • if the user is supposed to guess the number than your program works correctly. The reason it always "stops" at 86 is because 86 is the number generated. You guess the number - the program ends. Why it always generates this number is expained in the other comments and answers

    – Milo Bem
    Nov 21 '18 at 14:28











  • @MiloBem Oh my bad. The user is supposed to be guessing the number generated by the program and it prints out a bunch of random numbers till it reaches the result.

    – Marcos Z.
    Nov 21 '18 at 14:29














2












2








2








I did look at the threads pertaining to randoms and implemented them into this assignment but I have 2 questions.



1) I need my program to generate random numbers (and print them) and count the iterations. I have counting the iterations down but I don't know why the random numbers don't print out. Does it have something to do with my guess = 0? Here's an example if I'm not clear.



Example: 
Enter a number: 13
85
89
73
94
13
This took 5 tries


2) I have no clue why my program always ends up stuck at one number for the answer. The program immediately ends after entering the number 86.



import java.util.*; 

public class FeelingLucky {
public static void main (String args) {
Scanner sc = new Scanner (System.in);

int tries = 0;
int guess = 0;

Random random = new Random(1);
int num = random.nextInt(100) + 1;

System.out.print("Pick a number between 1 and 100:");

while (guess != num) {
guess = sc.nextInt();
tries++;
}
System.out.println("It took " + tries + " tries to match");
sc.close();
}
}









share|improve this question
















I did look at the threads pertaining to randoms and implemented them into this assignment but I have 2 questions.



1) I need my program to generate random numbers (and print them) and count the iterations. I have counting the iterations down but I don't know why the random numbers don't print out. Does it have something to do with my guess = 0? Here's an example if I'm not clear.



Example: 
Enter a number: 13
85
89
73
94
13
This took 5 tries


2) I have no clue why my program always ends up stuck at one number for the answer. The program immediately ends after entering the number 86.



import java.util.*; 

public class FeelingLucky {
public static void main (String args) {
Scanner sc = new Scanner (System.in);

int tries = 0;
int guess = 0;

Random random = new Random(1);
int num = random.nextInt(100) + 1;

System.out.print("Pick a number between 1 and 100:");

while (guess != num) {
guess = sc.nextInt();
tries++;
}
System.out.println("It took " + tries + " tries to match");
sc.close();
}
}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:43









Milo Bem

822418




822418










asked Nov 21 '18 at 14:11









Marcos Z.Marcos Z.

112




112













  • Since you seed your Random with (1), you always get the same sequence of numbers. You need to print out the numers to print them. ie System.out.println("the random number is: " + num);

    – matt
    Nov 21 '18 at 14:15













  • your description is confusing, is the program guessing the number entered by user or is the user guessing the number generated by the program?

    – Milo Bem
    Nov 21 '18 at 14:23











  • @matt It just ends up with the same result even if I print them.

    – Marcos Z.
    Nov 21 '18 at 14:28











  • if the user is supposed to guess the number than your program works correctly. The reason it always "stops" at 86 is because 86 is the number generated. You guess the number - the program ends. Why it always generates this number is expained in the other comments and answers

    – Milo Bem
    Nov 21 '18 at 14:28











  • @MiloBem Oh my bad. The user is supposed to be guessing the number generated by the program and it prints out a bunch of random numbers till it reaches the result.

    – Marcos Z.
    Nov 21 '18 at 14:29



















  • Since you seed your Random with (1), you always get the same sequence of numbers. You need to print out the numers to print them. ie System.out.println("the random number is: " + num);

    – matt
    Nov 21 '18 at 14:15













  • your description is confusing, is the program guessing the number entered by user or is the user guessing the number generated by the program?

    – Milo Bem
    Nov 21 '18 at 14:23











  • @matt It just ends up with the same result even if I print them.

    – Marcos Z.
    Nov 21 '18 at 14:28











  • if the user is supposed to guess the number than your program works correctly. The reason it always "stops" at 86 is because 86 is the number generated. You guess the number - the program ends. Why it always generates this number is expained in the other comments and answers

    – Milo Bem
    Nov 21 '18 at 14:28











  • @MiloBem Oh my bad. The user is supposed to be guessing the number generated by the program and it prints out a bunch of random numbers till it reaches the result.

    – Marcos Z.
    Nov 21 '18 at 14:29

















Since you seed your Random with (1), you always get the same sequence of numbers. You need to print out the numers to print them. ie System.out.println("the random number is: " + num);

– matt
Nov 21 '18 at 14:15







Since you seed your Random with (1), you always get the same sequence of numbers. You need to print out the numers to print them. ie System.out.println("the random number is: " + num);

– matt
Nov 21 '18 at 14:15















your description is confusing, is the program guessing the number entered by user or is the user guessing the number generated by the program?

– Milo Bem
Nov 21 '18 at 14:23





your description is confusing, is the program guessing the number entered by user or is the user guessing the number generated by the program?

– Milo Bem
Nov 21 '18 at 14:23













@matt It just ends up with the same result even if I print them.

– Marcos Z.
Nov 21 '18 at 14:28





@matt It just ends up with the same result even if I print them.

– Marcos Z.
Nov 21 '18 at 14:28













if the user is supposed to guess the number than your program works correctly. The reason it always "stops" at 86 is because 86 is the number generated. You guess the number - the program ends. Why it always generates this number is expained in the other comments and answers

– Milo Bem
Nov 21 '18 at 14:28





if the user is supposed to guess the number than your program works correctly. The reason it always "stops" at 86 is because 86 is the number generated. You guess the number - the program ends. Why it always generates this number is expained in the other comments and answers

– Milo Bem
Nov 21 '18 at 14:28













@MiloBem Oh my bad. The user is supposed to be guessing the number generated by the program and it prints out a bunch of random numbers till it reaches the result.

– Marcos Z.
Nov 21 '18 at 14:29





@MiloBem Oh my bad. The user is supposed to be guessing the number generated by the program and it prints out a bunch of random numbers till it reaches the result.

– Marcos Z.
Nov 21 '18 at 14:29












3 Answers
3






active

oldest

votes


















3














Random(1) uses seed in constructor which is always the same. Use just Random() - no parameter constructor.



import java.util.*;

public class FeelingLucky {
public static void main(String args) {

Scanner sc = new Scanner(System.in);

int tries = 0;
int guess = 0;

Random random = new Random(); // No seed
int num = random.nextInt(100) + 1;

System.out.print("Pick a number between 1 and 100:");

while (guess != num) {
guess = sc.nextInt();
tries++;
}
System.out.println("It took " + tries + " tries to match");
sc.close();

}
}


enter image description here



See Java random always returns the same number when I set the seed?






share|improve this answer


























  • Well the thing is this is a school assignment and I have no say in the matter because the professor wants it to have a seed value of 1. Thanks anyways though.

    – Marcos Z.
    Nov 21 '18 at 14:25











  • Seed is actually a way of getting same number sequence each time (kind of initial setup for Random) so it's not an issue but by design.

    – Akceptor
    Nov 21 '18 at 14:28



















0














You've called nextInt() on the Random object only once, so you've only generated the one random number. Inside your loop you call nextInt() on the scanner, which is reading from System.in, so your program is halting and waiting for the user to input a number again each time around the loop.



If you want the user to enter a single number once, and then Random to keep generating numbers until they match, you'd need to swap which one is called inside the loop. To print the random numbers being generated, you'd need to add a print statement inside the loop that prints that current number.






share|improve this answer































    0














    while (guess != num) {
    num = random.nextInt(100) + 1;
    guess = sc.nextInt();
    System.out.printf("you guessed: %d the number was %d%n",guess, num);

    tries++;
    }


    This one will print out each time, and guess a new random number each time.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413963%2fgenerating-randoms-but-a-few-minor-errors%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Random(1) uses seed in constructor which is always the same. Use just Random() - no parameter constructor.



      import java.util.*;

      public class FeelingLucky {
      public static void main(String args) {

      Scanner sc = new Scanner(System.in);

      int tries = 0;
      int guess = 0;

      Random random = new Random(); // No seed
      int num = random.nextInt(100) + 1;

      System.out.print("Pick a number between 1 and 100:");

      while (guess != num) {
      guess = sc.nextInt();
      tries++;
      }
      System.out.println("It took " + tries + " tries to match");
      sc.close();

      }
      }


      enter image description here



      See Java random always returns the same number when I set the seed?






      share|improve this answer


























      • Well the thing is this is a school assignment and I have no say in the matter because the professor wants it to have a seed value of 1. Thanks anyways though.

        – Marcos Z.
        Nov 21 '18 at 14:25











      • Seed is actually a way of getting same number sequence each time (kind of initial setup for Random) so it's not an issue but by design.

        – Akceptor
        Nov 21 '18 at 14:28
















      3














      Random(1) uses seed in constructor which is always the same. Use just Random() - no parameter constructor.



      import java.util.*;

      public class FeelingLucky {
      public static void main(String args) {

      Scanner sc = new Scanner(System.in);

      int tries = 0;
      int guess = 0;

      Random random = new Random(); // No seed
      int num = random.nextInt(100) + 1;

      System.out.print("Pick a number between 1 and 100:");

      while (guess != num) {
      guess = sc.nextInt();
      tries++;
      }
      System.out.println("It took " + tries + " tries to match");
      sc.close();

      }
      }


      enter image description here



      See Java random always returns the same number when I set the seed?






      share|improve this answer


























      • Well the thing is this is a school assignment and I have no say in the matter because the professor wants it to have a seed value of 1. Thanks anyways though.

        – Marcos Z.
        Nov 21 '18 at 14:25











      • Seed is actually a way of getting same number sequence each time (kind of initial setup for Random) so it's not an issue but by design.

        – Akceptor
        Nov 21 '18 at 14:28














      3












      3








      3







      Random(1) uses seed in constructor which is always the same. Use just Random() - no parameter constructor.



      import java.util.*;

      public class FeelingLucky {
      public static void main(String args) {

      Scanner sc = new Scanner(System.in);

      int tries = 0;
      int guess = 0;

      Random random = new Random(); // No seed
      int num = random.nextInt(100) + 1;

      System.out.print("Pick a number between 1 and 100:");

      while (guess != num) {
      guess = sc.nextInt();
      tries++;
      }
      System.out.println("It took " + tries + " tries to match");
      sc.close();

      }
      }


      enter image description here



      See Java random always returns the same number when I set the seed?






      share|improve this answer















      Random(1) uses seed in constructor which is always the same. Use just Random() - no parameter constructor.



      import java.util.*;

      public class FeelingLucky {
      public static void main(String args) {

      Scanner sc = new Scanner(System.in);

      int tries = 0;
      int guess = 0;

      Random random = new Random(); // No seed
      int num = random.nextInt(100) + 1;

      System.out.print("Pick a number between 1 and 100:");

      while (guess != num) {
      guess = sc.nextInt();
      tries++;
      }
      System.out.println("It took " + tries + " tries to match");
      sc.close();

      }
      }


      enter image description here



      See Java random always returns the same number when I set the seed?







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 21 '18 at 14:24









      secret super star

      943114




      943114










      answered Nov 21 '18 at 14:18









      AkceptorAkceptor

      1,2621424




      1,2621424













      • Well the thing is this is a school assignment and I have no say in the matter because the professor wants it to have a seed value of 1. Thanks anyways though.

        – Marcos Z.
        Nov 21 '18 at 14:25











      • Seed is actually a way of getting same number sequence each time (kind of initial setup for Random) so it's not an issue but by design.

        – Akceptor
        Nov 21 '18 at 14:28



















      • Well the thing is this is a school assignment and I have no say in the matter because the professor wants it to have a seed value of 1. Thanks anyways though.

        – Marcos Z.
        Nov 21 '18 at 14:25











      • Seed is actually a way of getting same number sequence each time (kind of initial setup for Random) so it's not an issue but by design.

        – Akceptor
        Nov 21 '18 at 14:28

















      Well the thing is this is a school assignment and I have no say in the matter because the professor wants it to have a seed value of 1. Thanks anyways though.

      – Marcos Z.
      Nov 21 '18 at 14:25





      Well the thing is this is a school assignment and I have no say in the matter because the professor wants it to have a seed value of 1. Thanks anyways though.

      – Marcos Z.
      Nov 21 '18 at 14:25













      Seed is actually a way of getting same number sequence each time (kind of initial setup for Random) so it's not an issue but by design.

      – Akceptor
      Nov 21 '18 at 14:28





      Seed is actually a way of getting same number sequence each time (kind of initial setup for Random) so it's not an issue but by design.

      – Akceptor
      Nov 21 '18 at 14:28













      0














      You've called nextInt() on the Random object only once, so you've only generated the one random number. Inside your loop you call nextInt() on the scanner, which is reading from System.in, so your program is halting and waiting for the user to input a number again each time around the loop.



      If you want the user to enter a single number once, and then Random to keep generating numbers until they match, you'd need to swap which one is called inside the loop. To print the random numbers being generated, you'd need to add a print statement inside the loop that prints that current number.






      share|improve this answer




























        0














        You've called nextInt() on the Random object only once, so you've only generated the one random number. Inside your loop you call nextInt() on the scanner, which is reading from System.in, so your program is halting and waiting for the user to input a number again each time around the loop.



        If you want the user to enter a single number once, and then Random to keep generating numbers until they match, you'd need to swap which one is called inside the loop. To print the random numbers being generated, you'd need to add a print statement inside the loop that prints that current number.






        share|improve this answer


























          0












          0








          0







          You've called nextInt() on the Random object only once, so you've only generated the one random number. Inside your loop you call nextInt() on the scanner, which is reading from System.in, so your program is halting and waiting for the user to input a number again each time around the loop.



          If you want the user to enter a single number once, and then Random to keep generating numbers until they match, you'd need to swap which one is called inside the loop. To print the random numbers being generated, you'd need to add a print statement inside the loop that prints that current number.






          share|improve this answer













          You've called nextInt() on the Random object only once, so you've only generated the one random number. Inside your loop you call nextInt() on the scanner, which is reading from System.in, so your program is halting and waiting for the user to input a number again each time around the loop.



          If you want the user to enter a single number once, and then Random to keep generating numbers until they match, you'd need to swap which one is called inside the loop. To print the random numbers being generated, you'd need to add a print statement inside the loop that prints that current number.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 14:19









          tdimmigtdimmig

          360420




          360420























              0














              while (guess != num) {
              num = random.nextInt(100) + 1;
              guess = sc.nextInt();
              System.out.printf("you guessed: %d the number was %d%n",guess, num);

              tries++;
              }


              This one will print out each time, and guess a new random number each time.






              share|improve this answer




























                0














                while (guess != num) {
                num = random.nextInt(100) + 1;
                guess = sc.nextInt();
                System.out.printf("you guessed: %d the number was %d%n",guess, num);

                tries++;
                }


                This one will print out each time, and guess a new random number each time.






                share|improve this answer


























                  0












                  0








                  0







                  while (guess != num) {
                  num = random.nextInt(100) + 1;
                  guess = sc.nextInt();
                  System.out.printf("you guessed: %d the number was %d%n",guess, num);

                  tries++;
                  }


                  This one will print out each time, and guess a new random number each time.






                  share|improve this answer













                  while (guess != num) {
                  num = random.nextInt(100) + 1;
                  guess = sc.nextInt();
                  System.out.printf("you guessed: %d the number was %d%n",guess, num);

                  tries++;
                  }


                  This one will print out each time, and guess a new random number each time.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 14:48









                  mattmatt

                  4,2551923




                  4,2551923






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413963%2fgenerating-randoms-but-a-few-minor-errors%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin