Generate a random number between a desired range using Java [duplicate] - java

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
I was able to generate a random number between [0,50] in my java code but how do I proceed in order to create for example a number in the range of [1,49]
This is my code:
public class Totoloto
{
public static void main(String[] args)
{
int n = (int) (Math.random()*50);
System.out.println("Number generated: "+n);
}
}

To get a random number from 1-49, you should pick a random number between 0-48, then add 1:
int min=1;
int max=49;
Random random=new Random();
int randomnumber=random.nextInt(max-min)+min;

Make use of the Random class. If you have a method designed as generateRandom(int min, int max) then you could create it like this
private static Random r = new Random();
public static void main(String[] args) {
for(int i = 0;i<10; ++i)
System.out.println(generateRandom(-1,1));
}
private static int generateRandom(int min, int max) {
// max - min + 1 will create a number in the range of min and max, including max. If you don´t want to include it, just delete the +1.
// adding min to it will finally create the number in the range between min and max
return r.nextInt(max-min+1) + min;
}

You can use a slightly different idiom of randomization:
Random r = new Random();
while (true) {
// lower bound is 0 inclusive, upper bound is 49 exclusive
// so we add 1
int n = r.nextInt(49) + 1;
System.out.println("Number generated: "+n);
}
Will print an infinite list of random numbers between 1 and 49.
Equivalent Java 8 idiom:
r.ints(0, 49).forEach((i) -> {System.out.println(i + 1);});

Try using the Random class from the util package:
Random r = new Random();
int n = r.nextInt(49) + 1;

Related

Generate random numbers with the first one being zero

I know how to get a range of random numbers between 0 zero and any number.
But what I want to know is, since the random number generator is not truly random, and follows a specific algorithm, for example if you pass a seed of 20. then it will always generate the same sequence of numbers: 17, 292, 0, 9.
So I get that. Since it follows a specific algorithm, is there a way I can force the generator to always start at zero or any other number?
But specifically zero in my case.
No need to hack the Random class, just write your own:
public class RandomGenerator {
private int bound;
private Random random;
private boolean firstCall = true;
public RandomGenerator(int bound, long seed) {
this.bound = bound;
random = new Random(seed)
}
public int next() {
if (firstCall) {
firstCall = false;
return 0;
}
return random.nextInt(bound);
}
}
public static void main (String[] args) throws java.lang.Exception
{
int x = -1;
long seed = 0;
int xxx = 100;
while(x!=0){
Random s = new Random(seed++);
x = s.nextInt(xxx);
}
System.out.println("seed " + (seed-1) + " gives " + new Random(seed-1).nextInt(xxx));
}
This will find a seed that the next int will be zero for a given modulus. (Happens to be 18 for this example).

How to avoid creating duplicates in array made by math.random numbers in Java? [duplicate]

This question already has answers here:
Generating Unique Random Numbers in Java
(21 answers)
Closed 4 years ago.
I have a method that creates an array with a length decided by a parameter in Main. The numbers are created by math random. How do I avoid duplicated by checking the created random number before putting it inside the array each time?
Is it possible to check an unfinished array?
class Test{
public int[] numberArray;
public int length;
public Test(int lengthArray){
this.numberArray=new int[lengthArray];
this.length=lengthArray;
}
public boolean checkArray(int checknumber){
inArray=false;
//what code can I write here to find duplicate
return inArray;
}
public void fillArray(){
int highest = 1000;
int lowest = 100;
int randomNumber = 0;
int counter=0;
for(int i=0;i<length;i++){
randomNumber=lowest + (int)(Math.random() * ((highest - lowest) + 1));
numberArray[counter]=randomNumber;
counter++;
}
}
public class testing {
public static void main(String[] args) {
Test test1 = new Test(9);
test1.fillArray();
for(int value: test1.numberArray){
System.out.print(value+" ");
}
}
}
You could first create all the numbers you need and store in a Set data structure till size equals the number of numbers you want. Later, transfer all the numbers in the Set to an array of the same size

How to generate 7 pseudorandom numbers with seed in 1 execution in java

Question is: Seed is 10L, Generate and print 7 pseudorandom integer values between 0 (inclusive) and 200 (exclusive).
Code I tried:
public static int randomGenerator() {
Random generator = new Random(10L);
return generator.nextInt(200 - 0) + 0;
}
System.out.println(randomGenerator());
To produce 7 numbers, you have to call the nextInt method 7 times without creating a new Random instance. To return all 7 values "in one execution", you need to return an array or a List.
I would also recommend giving your method some parameters, so that it isn't tied to those specs.
public static int[] getRandomInts(long seed, int amount, int min, int max) {
Random rand = new Random(seed);
int[] nums = new int[amount];
for (int i = 0; i < amount; i++) {
nums[i] = rand.nextInt(max - min) + min;
}
return nums;
}
In Java 8, the Random class has the alternative ints method that returns an IntStream:
public static int[] getRandomInts(long seed, int amount, int min, int max) {
return new Random(seed).ints(amount, min, max).toArray();
}
Then in your calling scope iterate over the numbers to print them.

How can I prevent the overlapping random numbers

How would i prevent duplicating numbers from random numbers.
I need to generate 5 numbers between 1 and 9 that are each different.
I would often get same numbers like 23334, how can i prevent that?
Any help would be great!
int num2 = (int) Math.round((Math.random()*9) +1);
int num1 = (int) Math.round((Math.random()*9) +1);
int num5 = (int) Math.round((Math.random()*9) +1);
int num3 = (int) Math.round((Math.random()*9) +1);
int num4 = (int) Math.round((Math.random()*9) +1);
One option is to use shuffle algorithm (e.g. Fisher-Yates shuffle ) to generate random sequence from 1 to 9, then take first 5 numbers of the sequence
Further explanation on StackOverflow: https://stackoverflow.com/a/196065/950427
Set<Integer> set=new HashSet<>();
while (set.size()<5) {
set.add( Math.round((Math.random()*9) +1));
}
After the set is filled you have 5 unique random numbers.
UPDATE: just to illustrate Jared Burrows' comment
Create a List includes the numbers that you want (1 to 9).
Generate random number from 0 to (size of the list minus 1).
Remove one element by index from the above generated random number. And add the removed element to a array which to be returned as a results
public static void main(String[] args) {
int []answers= returnRandomNonRepeatingNumbers(5,0,9);
for(int answer: answers) {
System.out.println(answer);
}
}
public static int[] returnRandomNonRepeatingNumbers(int sizeYouWant, int poolStart, int poolEnd) {
List<Integer> pool=new ArrayList<Integer>();
for(int i=poolStart;i<=poolEnd;i++) {
pool.add(i);
}
int []answers=new int[sizeYouWant];
for(int i=0;i<sizeYouWant;i++) {
//random index to be pick and remove from pool
int randomIndex = (int) Math.round((Math.random()*(pool.size()-1)));
answers[i]=pool.remove(randomIndex);
}
return answers;
}
If the number of possible random values is small, you want to use shuffle.
List<Integer> values = IntStream.range(0, 10).boxed().collect(toList());
Collections.shuffle(values);
values = values.subList(0, 5);
If the number of possible random values is large, you want to test adding them to a Set (or the original list if small enough)
Set<Integer> valueSet = new HashSet<>();
Random rand = new Random();
while(valuesSet.size() < 5) valuesSet.add(rand.nextInt(9) + 1);
List<Integer> values = new ArrayList<>(valueSet);
Collections.shuffle(values, rand);
Note: you need to shuffle the set as it doesn't preserve order. e.g. the numbers 1,2,3 will always come out in that order with HashSet, not 3,2,1.
Floyd's subset selection algorithm is designed to do exactly what you want, and is extremely efficient even for large sets. Selecting m items from a set of n is O(m) average running time, independent of n. Here's a Java implementation.
/*
* Floyd's algorithm to chose a random subset of m integers
* from a set of n, zero-based.
*/
public static HashSet<Integer> generateMfromN(int m, int n) {
HashSet<Integer> s = new HashSet<Integer>();
for (int j = n-m; j < n; ++j) {
if(! s.add((int)((j+1) * Math.random()))) {
s.add(j);
}
}
return s;
}
One possible approach to this problem can be divide & conquer. Step of following describes the approach:
Say m is the minimum & n is the maximum, within what i wanna get x number of randoms
Choose a random p between m & n. Save it to an array of answer. decrease x by 1 as we get one answer to our problem.
Now take a q a random number between m & p-1, another r a random number between p+1 & n. Fill up the answer array with q & r decrease x 1 for q and another 1 for the r.
Now carry on this process recursively, until the lower bound (m) & higher bound (n) becomes equal or x becomes 0.
Benefit: benefit of this approach is that, in worst case, it's runtime will be O(x), where x is the number of random number required. The best case scenarion is also o(x), as i have to find at least n number of random. These two comprise average case to θ(x) complexity.
import java.util.Random;
class GenerateDistinctRandom{
static int alreadyPut = 0;
static Random rand = new Random();
public static int[] generateDistinctRandom(int howMany, int rangeMin, int rangeMax)
{
int randomNumbers[] = new int[howMany];
GenerateDistinctRandom.recursiveRandomGenerator(rangeMin, rangeMax, randomNumbers, howMany);
return randomNumbers;
}
private static void recursiveRandomGenerator(int rangeMin, int rangeMax, int[] storage ,int storageSize)
{
if(rangeMax - rangeMin <= 0 || GenerateDistinctRandom.alreadyPut == storageSize)
{
return ;
}
int randomNumber = GenerateDistinctRandom.rand.nextInt(rangeMax-rangeMin) + rangeMin;
storage[GenerateDistinctRandom.alreadyPut] = randomNumber;
GenerateDistinctRandom.alreadyPut++;
//calling the left side of the recursion
recursiveRandomGenerator(rangeMin, randomNumber - 1, storage, storageSize);
recursiveRandomGenerator(randomNumber + 1, rangeMax, storage, storageSize);
}
public static void main(String []args){
int howMany = 5;
int distinctNumber[] = GenerateDistinctRandom.generateDistinctRandom(howMany 0, 9);
for(int i = 0;i < howMany;i++)
{
System.out.println(distinctNumber[i]);
}
}
}
I suppose you would need to store the ones that have been generated into an array and compare the new random number to the list to ensure it is unique.
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] numbers = new int[5];
int tempNumber = 0;
for(int numberCounter = 0; numberCounter < numbers.length;)
{
tempNumber = (int) Math.round((Math.random()*9) +1);
if(!contains(numbers, tempNumber)){
numbers[numberCounter++] = tempNumber;
}
}
}
public static boolean contains(final int[] numbersArray, final int tempNumber) {
for (final int numberFromArray : numbersArray) {
if (numberFromArray == tempNumber) {
return true;
}
}
return false;
}
I notice you did not use an array in your example, so in case you do not know how to use them yet, you could also make 5 variables.
int randomNumber = 0;
int firstNumber = Math.round((Math.random()*9) +1);
int secondNumber = 0;
while(secondNumber == 0){
randomNumber = Math.round((Math.random()*9) +1)l
if(randomNumber != firstNumber){
secondNumber = randomNumber;
}
}
And you could continue making while statements like that. But if you are supposed to know about arrays, you should definitely be using one to store the numbers.
How about this?
package com.se;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TestRandom {
List<Integer> comp = new ArrayList<>();
int listSize = 20;
public void doTask() {
Random ran = new Random();
int i = 0;
while(i < listSize){
int randomNumber = ran.nextInt(80) + 1;
if(!comp.contains(randomNumber)){
comp.add(randomNumber);
i++;
}
}
for(Integer num : comp){
System.out.println(num);
}
}
public static void main(String[] args) {
TestRandom testRandom = new TestRandom();
testRandom.doTask();
}
}

Generating Random Numbers from 1 to n-1 for BigIntegers in Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to generate a random BigInteger value in Java?
I'm using BigInteger class in JAVA, and I want to generate random numbers from 1 to x-1 . I'm not sure how to do that?
I cannot use nextInt() since it only accepts int not BigIntegers also, I'll be generating 32bit and above numbers so, even nextInt() will not work.
I know If I search I may find some helpful solutions, but I'm so short in time. (2 hours before deadline)
Thanks in Advance.
May this work
public static void main(String[] args) {
BigInteger bigInteger = new BigInteger("9349988899999");
BigInteger bigInteger1 = bigInteger.subtract(new BigInteger("1"));
System.out.println(randomBigInteger(bigInteger1));
}
public static BigInteger randomBigInteger(BigInteger n) {
Random rnd = new Random();
int maxNumBitLength = n.bitLength();
BigInteger aRandomBigInt;
do {
aRandomBigInt = new BigInteger(maxNumBitLength, rnd);
// compare random number lessthan ginven number
} while (aRandomBigInt.compareTo(n) > 0);
return aRandomBigInt;
}
Just use the constructor that get Random
Random rnd = new Random();
BigInteger i = new BigInteger(maxNumOfBits, rnd);
If you want between 0 and some number, you can generate the same number of bits as that number and generate random numbers until you get one lesser then it.
(Code not tested)
public static BigInteger randomBigInteger(BigInteger maxNum) {
Random rnd = new Random();
int maxNumBitLength = maxNum.bitLength();
BigInteger rndNum;
do {
rndNum = new BigInteger(maxNumBitLength, nd);
} while(result.compareTo(upperLimit) >= 0);
return result;
}

Categories