Create a random random number between a given range

How to create an odd random number between a given range.

For example: for a range from 1 to 6. Random No - 3 or 1 or 5

Random Number Generation Method:

Random_No = Min + (int)(Math.Random()*((Max-Min)+1)) 

Refer How to generate random integers in a specific range in Java?

Random random number creation method:

  Random_No = Min + (int)(Math.Random()*((Max-Min)+1)) if(Random_No%2 ==0) { if((Max%2)==0)&&Random_No==Max) { Random_No = Random_No - 1; } else{ Random_No = Random_No +1; } } 

This function will always convert 2 to 3, not 1. Can we make this a more random function that can convert 2 sometimes to 3, and sometimes to 1 ??

+7
source share
10 answers

Assuming max is enabled, I would suggest the following:

 if (Max % 2 == 0) --Max; if (Min % 2 == 0) ++Min; Random_No = Min + 2*(int)(Math.random()*((Max-Min)/2+1)); 

This results in a uniform distribution among all odd numbers.

+7
source

If you want to include randomness in a direction, also use a random number for the same.

  int randomDirection = Min + (int)(Math.Random()*((Max-Min)+1)); if(randomDirection%2==0) { // any condition to switch the direction Random_No = Random_No + 1; } else { Random_No = Random_No - 1; } 
+2
source

Instead of generating a random number between 0 and 6, create one between 0 and 5 and round to the nearest odd number, so you get the perfect distribution (33% for each possibility (1, 3, 5))

+1
source

To do this, you need to create a second pseudo-random number to add or subtract 1

 Random_No = Min + (int)(Math.Random()*((Max-Min)+1)) repartitionNumber =(int)(Math.Random()*((2)) // between 0 and 1 if(Random_No%2 ==0) { if(Random_No+1<=Max && Random_No-1>=Min) { if(repartitionNumber==0) Random_No = Random_No + 1; else Random_No = Random_No - 1; } else if(Random_No+1<=Max) Random_No = Random_No + 1; else if (Random_No-1>=Min) Random_No = Random_No - 1; } 
+1
source

I wonder why other answers use int cast to generate a random number. Why not create a random integer directly, which is more accurate than a real number?

 Random rn = new Random(); if(maximum % 2 == 1) maximum = maximum + 1; // turn right bound to even if(minimum % 2 == 0) minimum = minimum - 1; // turn left bound to odd int range = (maximum - minimum + 1) / 2; int randomNum = rn.nextInt(range) * 2 + minimum; 
+1
source

To generate an odd number from an integer, you can use n * 2 + 1 Indeed, you generate random numbers and apply the conversion afterwards

 int num = min / 2 + random.nextInt((max + 1) / 2 - min / 2); num = num * 2 + 1; 

This will work even if the range is [1.5] [2.5] [2.6] [1.6]

+1
source

Mathematically, the numbers will not get anything, rounding up or down in the last step. Instead, the first and last numbers are 50% less likely to get all the other numbers.

Stick to CrazyCasta or JA

0
source

How about checking the return from Math.random () as a floating number. If its inside is an even number, then convert up / down based on its floating part. How:

suppose Math.random () is returned by xy; if x is even, return (y> = 0.5)? (x + 1) :( x-1)

Will it be randomized a bit?

0
source

Let the load higher or lower depend on the random epsilon.

  Random_No = Min + (int)(Math.Random()*((Max-Min)+1)) if(Random_No%2 ==0) { if((Max%2)==0)&&Random_No==Max) { Random_No = Random_No - 1; } else{ epsilon = Math.Random(); if(epsilon > 0.5) Random_No = Random_No + 1; else Random_No = Random_No - 1; } } 
0
source

In Java 1.7 or later, I would use ThreadLocalRandom :

 import java.util.concurrent.ThreadLocalRandom; // Get odd random number within range [min, max] // Start with an odd minimum and add random even number from the remaining range public static int randOddInt(int min, int max) { if (min % 2 == 0) ++min; return min + 2*ThreadLocalRandom.current().nextInt((max-min)/2+1); } // Get even random number within range [min, max] // Start with an even minimum and add random even number from the remaining range public static int randEvenInt(int min, int max) { if (min % 2 != 0) ++min; return min + 2*ThreadLocalRandom.current().nextInt((max-min)/2+1); } 

The reason for using ThreadLocalRandom is explained here . Also note that the reason we added +1 to the ThreadLocalRandom.nextInt () input is to make sure max is included in the range.

0
source

All Articles