How to Generate Random Integers Within a Specific Range in Java

Generate random numbers within specific range and with given conditions in javascript - times table

I think this is what you are looking for. I took a completely different approach.

First, I'm using a Map because it is simple to ensure uniqueness. (keys must be unique)

Note I'm using a simple string for the key to ensure they are unique (using objects there get's a bit more complicated)

The ranges array represents your 'number2, number3` etc.

The loop starts at 2 to satisfy your multiplier range (2 - 11). This requires a bit of trickery to get index cone correctly.

This generates unique pairs (as key) and the value of the map is the generated value and the multiplier multiplied together.

The size and Map are printed in the console.

const randomNumbersGenerator = () => {
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
const randomNumber = () => getRandomInt(2, 12);
const multiplierMap = new Map();
const ranges = [[0, 2], [0, 3], [0, 3], [0, 3], [0, 3], [1, 4], [1, 4], [1, 4], [1, 4], [0, 2], [0, 3]];
while(multiplierMap.size < 17) {
for (let i = 2; i < ranges.length+1; i++) {
const randomInt = randomNumber();
if(Array.from(multiplierMap.values).includes(randomInt * i)){
--i;
} else {
multiplierMap.set(randomInt + " " + i, randomInt * i);
}
if(multiplierMap.size === 18) break;
}
}
console.log('MultiplierMap size: ', multiplierMap.size);
for(let [pair, multiple] of multiplierMap){
console.log('Generated Multiplier: ' + pair, '||', 'Generated * Multiplier: ' + multiple);
}
return multiplierMap;

};
randomNumbersGenerator();

Get random numbers in a specific range in java

Random rand = new Random(seed);
int random_integer = rand.nextInt(upperbound-lowerbound) + lowerbound;

Random Number generator in a range and then print length of the sequence

Consider using a while-loop:

import java.util.Random;

class Sequence {
public static void main(String[] args) {
Random r = new Random();
int count = 0;
int num = -1;
System.out.println("The sequence is:");
while (num != 0) {
num = r.nextInt(10);
System.out.print(num + " ");
count++;
}
System.out.printf("%nThe length of the sequence is: %d%n", count);
}
}

Example Output:

The sequence is:
3 9 3 9 4 0
The length of the sequence is: 6

Alternatively if you need to store the sequence in a collection:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Sequence {
public static void main(String[] args) {
Random r = new Random();
List<Integer> result = new ArrayList<>();
int num = -1;
while (num != 0) {
num = r.nextInt(10);
result.add(num);
}
System.out.printf("The sequence is: %s%n", result);
System.out.printf("The length of the sequence is: %d%n", result.size());
}
}

How to generate random integers in a range

You could do

pepe = rng.nextInt(3) - 1;

rng.nextInt(3) returns a random element in the set {0, 1, 2} - therefore subtracting 1 returns a random element in the set {-1, 0, 1}, as desired.


Relevant documentation:

  • nextInt


Related Topics



Leave a reply



Submit