get random char in a specific range in java [duplicate] - java

This question already has answers here:
Is there functionality to generate a random character in Java?
(20 answers)
Closed 6 years ago.
Am designing wumpus hunting program using java. Where i can include three gold, 3 pits, one wumpus, one player. and the position of the game item should be random generator.How to generate random characters in 2d array .
How to specify the range for the characters in java.
Thanks.

You can use the method random.nextInt() to generate a random number for the position.
If you want to generate a random character you should use :
(char)(random.nextInt(max-min)+min). Where the max and min value are the ascii code of the max and the min char.
If you only want to generate a char in some char you should create an array with these character and use yourArray[random.nextInt(yourArray.length-1)+1];
Edit : You have to create the object random at the beginning of your
code. Random random = new Random();

Related

(Java) How do you generate a random integer between 1 to 4 using SecureRandom [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 3 years ago.
An assignment I'm working on right now needs to generate a random number from 1 to 4 using SecureRandom that will be used for a switch statement. Cases have to start from 1 so I can't just use
SecureRandom rand = new SecureRandom();
int a;
a = rand.nextInt(4);
right?
I have to use SecureRandom to generate the integer as well.
You're off by one (a very common source of errors). Of note is that rand.nextInt(n) will return a value from 0 to n - 1. You want
int a = 1 + rand.nextInt(4);
Also, make sure you reuse that SecureRandom instance (don't recreate it in a loop for example).

Is there a java equivalent of python random._urandom()? [duplicate]

This question already has answers here:
How to create an array of 20 random bytes?
(6 answers)
Getting random numbers in Java [duplicate]
(2 answers)
Closed 3 years ago.
If java has a function similar to the function in python called urandom from the random library how can I use it?
Python you would do:
import random
random._urandom(1)
What can I do in java for this?
Urandom doesn't generate a random numbers, it returns n random bytes suitable for cryptographic use
Use:
byte[] bytes = new byte[x];
new Random().nextBytes(bytes);
Your array will then be filled with random bytes

Generating a three-digit number Java [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 3 years ago.
I using this code to generate two digit but I want to change it and make it generate three digit.
int lottery = (int)(Math.random() * 100);
Thanks
You only have to change the multiplication from 100 to 1000 :)
int lottery = (int)(Math.random() * 1000);

Java: Can Function have a parameter for an array of a specific length? [duplicate]

This question already has answers here:
Java Method with Enforced Array Size Parameters?
(3 answers)
Closed 5 years ago.
I am making a subroutine that will convert a 8-bit binary number into a int value. I want it to take as a parameter a int array with a length of 8 (to represent the binary number) and reject arrays of any other length. Is the possible?
It is not possible to ensure the length of an array at compile time. You have to use
if(my_array.length != 8){
throw new Exception("Array must have a length of 8.");
}

A java program that generates 100 random numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can someone help me with this exercise? Write a program that generates 100 random numbers and stores each number into an integer array. For this program, you must meet the following technical requirements: 1) In main, generate the 100 random numbers and store each in an array 2) In main, ask the user to enter a number to see if it exists in the array 3) Write a function that takes the array and user number as input parameters and returns either true or false if the number does or does not exist in the array respectively 4) In main, call this function appropriately and use the result to print out to the user if the number they provided is in the array
This is an extremely easy problem, and I will walk you through the steps, but not give you all the code because I know what it is like to be a beginner, and I learn visually by reading code.
You need an import for this, and that import is java.util.Random
You need an array to store it in, create an array with int[] and set it to a size of 100
You need a random object Random rand = new Random()
To generate 100 random integers and add them to an array you need to loop 100 times. Within that loop generate a number and add it to the array at index i
To check if a user inputted key exists once again loop through the array until a arr[i] == key, if it never finds a match it does not contain the number
If you have questions please leave a comment and I will try to be helpful
Use the java.util.Random class to generate random numbers like so:
Random randomGenerator = new Random();
int randomLessThan100 = randomGenerator.nextInt(100);
int randomLessThan1000 = randomGenerator.nextInt(1000);
int randomBetween50And100 = randomGenerator.nextInt(49) + 51;

Categories

Resources