Question

I would really like to find another way to do this program Random rand = new...

I would really like to find another way to do this program Random rand = new Random(); String choice = null; boolean valid = false; int a = rand.nextInt(10); int b = rand.nextInt(10); int c = rand.nextInt(10); int d = rand.nextInt(10); while (!valid) { // do{ // if (a == b) { rand.nextInt(10); } if (a == c) { rand.nextInt(10); } if (a == d) { rand.nextInt(10); } if (b == c) { rand.nextInt(10); } if (b == d) { rand.nextInt(10); } if (b == a) { rand.nextInt(10); } if (c == a) { rand.nextInt(10); } if (c == b) { rand.nextInt(10); } if (c == d) { rand.nextInt(10); if (d == a) { rand.nextInt(10); } if (d == b) { rand.nextInt(10); } if (d == c) { rand.nextInt(10); } // }while } } choice = Integer.toString(a)+ Integer.toString(b)+ Integer.toString(c)+ Integer.toString(d); Game g = new Game(); g.setRandomNumberGame(choice); dao.addGame(g); //create game object //save to dao return g; I just need help writing a program that generates a random 4 digit number where every digit is different. For each round, the user guesses a number and is told exact and partial matches

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

Random rand = new Random();

String choice = null;
int[] ints = new Random().ints(0, 10).distinct().limit(4).toArray();
int a = ints[0];
int b = ints[1];
int c = ints[2];
int d = ints[3];
choice = Integer.toString(a) + Integer.toString(b) + Integer.toString(c) + Integer.toString(d);
Game g = new Game();
g.setRandomNumberGame(choice);
System.out.println(choice);

Sample code :

import java.util.Random;
public class Game {
String randomNumber = "";

public static void main(String[] args) {
Random rand = new Random();

String choice = null;
int[] ints = new Random().ints(0, 10).distinct().limit(4).toArray();
int a = ints[0];
int b = ints[1];
int c = ints[2];
int d = ints[3];
choice = Integer.toString(a) + Integer.toString(b) + Integer.toString(c) + Integer.toString(d);
Game g = new Game();
g.setRandomNumberGame(choice);
System.out.println(choice);
// dao.addGame(g);
}

public void setRandomNumberGame(String number) {
this.randomNumber = number;
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
I would really like to find another way to do this program Random rand = new...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • **JAVA** Using Random rand = new Random(); Which of the following expression generates a random integer...

    **JAVA** Using Random rand = new Random(); Which of the following expression generates a random integer between 20 and 100, inclusive? A. rand.nextInt() % 80 + 20 B. rand.nextInt() % 81 C. rand.nextInt() % 80 D. rand.nextInt() % 81 + 20

  • How do I take a random char element from an existing array and assign it to...

    How do I take a random char element from an existing array and assign it to a new array? I have this code with a testing method but my code only returns an empty array instead of the chars expected. public static char[] generateHiddenCode(Random rand, int numPositions, char[] symbols) { char[] arrCode = new char[numPositions]; for (int i=0; i<arrCode.length; ++i) { arrCode[i] = (char) rand.nextInt(symbols.length); } return arrCode; } Test:    private static void testGenerateHiddenCode() { boolean error = false;...

  • Random rand = new Random( ); int x = rand.nextlnt(10); while (x < 9) ( //...

    Random rand = new Random( ); int x = rand.nextlnt(10); while (x < 9) ( // Point A x = rand.nextlnt(10); // Point B } // Point C Determine whether the following assertions are ALWAYS. SOMETIMES, or NEVER true at the indicated points. A. NEVER B. ALWAYS C. SOMETIMES

  • IVOVI U UL Question 46 Suppose we have Random rand = new Random(); Which of the...

    IVOVI U UL Question 46 Suppose we have Random rand = new Random(); Which of the following expression generates a random integer between 20 and 100, inclusive? rand.nextint() 96 80 rand.nextint() 96 80 + 20 rand.nextInt() % 81 rand.nextInt() % 81 + 20 A Moving to another question will save this response. Moving to another question will save this response. Question 47 Suppose I have the following Scanner object: File file = new File("data txt") Scanner input = new Scanner(file);...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • 4. Write a program that generates one hundred random integers between 0 and 9 and displays...

    4. Write a program that generates one hundred random integers between 0 and 9 and displays the count for each number. (Hint: Use rand()%10 to generate a random integer between 0 and 9. Use an array of ten integers, say counts, to store the counts for the number of 0’s, 1’s,…..,9’s.) this program kinda works but i cant figure out why its making more than 100 random numbers ls) [*] test2.cpp test3.cpp #include<stdio.h> #include<stdlib.h> int main() 4 { int a[14],is...

  • I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that...

    I need Help PLZ. I can't solving this in C program Given is a C program count_primes.c, which is an array Generates 10000 six-digit random numbers and then determines the number of primes in that array. It also measures the time that elapses during the calculation and returns it to the console along with the number of primes it finds. (a) Rewrite the program so that N threads are used to count the primes in the array. Each thread is...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT