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();
    }
}
Milo Bem
  • 1,033
  • 8
  • 20
Marcos Z.
  • 21
  • 2
  • 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
  • The issue here is your program always generates completely random number which is always 86. And that is because of `1` given in Random object constructor. As long as you have that value passed to a constructor you'll have that number generated. – Akceptor Nov 21 '18 at 14:32
  • @MarcosZ. do you want a new random number each time? – matt Nov 21 '18 at 14:46

3 Answers3

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?

Shiva
  • 1,962
  • 2
  • 13
  • 31
Akceptor
  • 1,914
  • 3
  • 26
  • 31
  • 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.

tdimmig
  • 680
  • 9
  • 24
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.

matt
  • 10,892
  • 3
  • 22
  • 34