Random Uniform Distribution

Gustavo Gomes Mendonca

So i want to get random elements of a list with uniform distribution in Java. I know that in the Random class, for example the nextInt method, already give me something like that:

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

So given something like the code below:

Random rnd = new Random();
int numTimes = 10;
for(int i = 0; i < numTimes*n; i++){
   System.out.println(rnd.nextInt(10));
}

I expect that for small "n" I can't quite see a good uniform distribution, probably increasing it, I will see something better. So my question is, how can I guarantee a uniform distribution within smaller n, or in other words, with "n = 2" how can I get every number at least once?


Trying to explain better: giving 10-number range dataset, and for example 20 iterations, is there a way that each number is printed 1-3 times, in other words, at least once?

Erwin Bolwidt

If you want to generate numbers that occur exactly the same number of times (which is not the same as a uniform distribution), then there is a better way to do it.

int n = 2; // your "n"
int t = 100; // how often you want each number x to occur, where 0 <= x < n

// Build a list of numbers
List<Integer> l = new ArrayList<>();
for (int i = 0; i < t; i++) {
    for (int j = 0; j < n; j++) {
        l.add(j);
    }
}
// Shuffle the list randomly; this ensures the order is random but each number x occurs
// as often as any other x
Collections.shuffle(l);

for (Integer value : l) {
    System.out.println(value);
}

If you want to have some numbers at least once, but don't care about the others; then insert 1 of each number that you want at least once and the rest randomly. If I understand you correctly, you want the numbers 1, 2 and 3 at least once, and then randomly numbers 1, 2 and 3. So, that would be:

int n = 3; // your "n"

// Build a list of numbers
List<Integer> l = new ArrayList<>();
for (int x = 1; x <= n; x++) {
     l.add(x);
}

int t = 17; // add 17 more random numbers in range 1-3 inclusive

for (int i = 0; i < t; i++) {
    l.add(rnd.nextInt(n) + 1);
}

// Shuffle
Collections.shuffle(l);

// Print
for (Integer value : l) {
    System.out.println(value);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Random Uniform Distribution

From Dev

Generating uniform distribution using Math.random()

From Dev

Random non-uniform distribution with given proportion

From Dev

Very fast uniform distribution random number generator

From Dev

Random vector a with elements from the uniform distribution in R

From Dev

Generating a random integer with non-uniform distribution

From Dev

Random real numbers with uniform distribution in verilog

From Dev

C++ thread-safe uniform distribution random number generation

From Dev

Generate random position with uniform distribution inside rounded rect

From Dev

How to code a non-uniform random distribution in PHP

From Dev

Generate random numbers with uniform distribution (getting same number in loop)

From Dev

is boost::random::uniform_real_distribution supposed to be the same across processors?

From Dev

Does casting random integers to short give a uniform distribution?

From Dev

Generate random numbers with uniform distribution (getting same number in loop)

From Dev

does random number in the real world follow uniform distribution

From Dev

Making own random number class compitible with uniform_int_distribution

From Dev

Why there is no uniform_distribution?

From Dev

Numpy Uniform Distribution With Decay

From Dev

Python fit uniform distribution

From Dev

Discrete uniform circular distribution

From Dev

Lots of compiler errors when attempting to use random_device, mt19937, and uniform_int_distribution

From Dev

How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

From Dev

How can I transform a random integer to a uniform real [0,1[ distribution

From Dev

How many random numbers can std::uniform_real_distribution generate before losing randomness?

From Dev

Do std::random_device and std::mt19937 follow an uniform distribution?

From Dev

How efficient is it to make a temporary uniform random distribution each time round a loop?

From Dev

Random numbers generated using uniform real distribution in C++ are not really uniformly distributed

From Dev

How to generate random uniform distribution by selecting h/l values from multiple arrays?

From Dev

Given a uniform distribution of a variable, use a function of random variables to plot the probability density function MATLAB

Related Related

  1. 1

    Random Uniform Distribution

  2. 2

    Generating uniform distribution using Math.random()

  3. 3

    Random non-uniform distribution with given proportion

  4. 4

    Very fast uniform distribution random number generator

  5. 5

    Random vector a with elements from the uniform distribution in R

  6. 6

    Generating a random integer with non-uniform distribution

  7. 7

    Random real numbers with uniform distribution in verilog

  8. 8

    C++ thread-safe uniform distribution random number generation

  9. 9

    Generate random position with uniform distribution inside rounded rect

  10. 10

    How to code a non-uniform random distribution in PHP

  11. 11

    Generate random numbers with uniform distribution (getting same number in loop)

  12. 12

    is boost::random::uniform_real_distribution supposed to be the same across processors?

  13. 13

    Does casting random integers to short give a uniform distribution?

  14. 14

    Generate random numbers with uniform distribution (getting same number in loop)

  15. 15

    does random number in the real world follow uniform distribution

  16. 16

    Making own random number class compitible with uniform_int_distribution

  17. 17

    Why there is no uniform_distribution?

  18. 18

    Numpy Uniform Distribution With Decay

  19. 19

    Python fit uniform distribution

  20. 20

    Discrete uniform circular distribution

  21. 21

    Lots of compiler errors when attempting to use random_device, mt19937, and uniform_int_distribution

  22. 22

    How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

  23. 23

    How can I transform a random integer to a uniform real [0,1[ distribution

  24. 24

    How many random numbers can std::uniform_real_distribution generate before losing randomness?

  25. 25

    Do std::random_device and std::mt19937 follow an uniform distribution?

  26. 26

    How efficient is it to make a temporary uniform random distribution each time round a loop?

  27. 27

    Random numbers generated using uniform real distribution in C++ are not really uniformly distributed

  28. 28

    How to generate random uniform distribution by selecting h/l values from multiple arrays?

  29. 29

    Given a uniform distribution of a variable, use a function of random variables to plot the probability density function MATLAB

HotTag

Archive