[Solved] JAVA – How do I generate a random number that is weighted towards certain numbers? [duplicate]

Introduction

Generating random numbers is a common task in programming, and it can be useful for a variety of applications. However, sometimes it is desirable to generate random numbers that are weighted towards certain numbers. This can be useful for creating a more realistic random distribution, or for creating a game with a higher chance of certain outcomes. In this article, we will discuss how to generate a random number that is weighted towards certain numbers using Java. We will discuss the different methods available, and provide examples of how to use them.

Solution

The following code snippet can be used to generate a random number that is weighted towards certain numbers:

// Create an array of weights
int[] weights = {1, 2, 3, 4, 5};

// Create a random number generator
Random rand = new Random();

// Generate a random number between 0 and the sum of the weights
int totalWeight = 0;
for (int weight : weights) {
totalWeight += weight;
}
int randomNumber = rand.nextInt(totalWeight);

// Find the index of the weight that the random number falls into
int index = 0;
int cumulativeWeight = 0;
for (int weight : weights) {
cumulativeWeight += weight;
if (randomNumber < cumulativeWeight) { break; } index++; } // The index is the weighted random number int weightedRandomNumber = index;

public static void main(String[] args){
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(5);list.add(2);// to be continued ...        
    int randomInt = list.get((int) (Math.random() * list.size()));
    System.out.println(randomInt);
}

You’ll get your List of values, and then you just need to pick randomly an index and get back the value

Math.random() will pick a value in [0;1[, if you multiply by the size of a List of 10 elements, you’ll a value in [0;10[ , when you cast as an int you’ll get 10 possibilities of index to look into the List

The ArrayList (implementation of List) allows duplicate element so :
if you add 1,2,2,3,4,5,6,7,8,9 (the order does not matter) you’ll have 10 elements with each one to be find with 1/10 = 10/100 = 10% of probability (aka chance), but the element 2 which appears twice will have 20% chance to be choosen in the method, more the number appears more it’s chance to be choosen increase, with as you said (number of time the number appears)/(total number of element) (this is basic maths)

6

solved JAVA – How do I generate a random number that is weighted towards certain numbers? [duplicate]

Generating a random number that is weighted towards certain numbers can be done in Java using the Random class. The Random class provides a method called nextInt(int n) which returns a random integer between 0 (inclusive) and the specified value (exclusive). To generate a random number that is weighted towards certain numbers, you can use the following approach:

  1. Create an array of integers that represent the weights of the numbers you want to generate.
  2. Create a new array that contains the cumulative sum of the weights.
  3. Generate a random number between 0 and the sum of the weights.
  4. Use a binary search to find the index of the random number in the cumulative sum array.
  5. Return the number at the index of the cumulative sum array.

For example, if you want to generate a random number that is weighted towards the numbers 1, 2, and 3, with weights of 0.2, 0.3, and 0.5 respectively, you can use the following code:

int[] weights = {0.2, 0.3, 0.5};
int[] cumulativeSum = new int[weights.length];
cumulativeSum[0] = weights[0];
for (int i = 1; i < weights.length; i++) {
    cumulativeSum[i] = cumulativeSum[i - 1] + weights[i];
}

Random random = new Random();
int randomNumber = random.nextInt(cumulativeSum[cumulativeSum.length - 1]);
int index = Arrays.binarySearch(cumulativeSum, randomNumber);
if (index < 0) {
    index = Math.abs(index + 1);
}
int number = index + 1;

In this example, the random number generated will be 1, 2, or 3 with the specified weights.