How do you generate a random integer in a specifie

2020-03-27 05:29发布

Given a range of integers, how do I generate a random integer divisible by 5 in that range?

I'm using Java

5条回答
再贱就再见
2楼-- · 2020-03-27 05:41

just generate a regular random integer and multiply it by 5!

details: generate a random integer in [0, n) where n is the number of multiples of 5 in your range, then multiply it by 5 and add the lowest multiple to it.

one-liner: System.out.println(rnd.nextInt(max / 5 - (min + 4) / 5 + 1) * 5 + (min + 4) / 5 * 5); (assuming non-negative and valid arguments)

credits: lowest multiple expression (min + 4) / 5 * 5 from here and expression simplified a bit based on @Thomas's (imo currently incorrect) answer

查看更多
该账号已被封号
3楼-- · 2020-03-27 05:49

This question calls for a multiple of five in a range, not number in the period of five in the range.

This solution handles negatives and range validity.

    // because Java's % operator doesn't do what one might expect with negatives

    int lbound = (min+4) - (((min+4) % 5) + 5) % 5;
    int ubound = max - (((max % 5) + 5) % 5);

    if (lbound > ubound) {
        // do something about the range error
    }

    if (lbound == ubound) {

        return lbound;
    }

    int range = ((ubound - lbound)/5) + 1;

    return ((int)(Math.random() * range) * 5) + lbound;
查看更多
时光不老,我们不散
4楼-- · 2020-03-27 05:53

First create a Random, and round low and high to the nearest higher/lower multiple of 5 respectively:

Random r = new Random();
low = ((low+4)/5)*5;    // next multiple of 5
high = (high/5)*5;  // previous multiple of 5

This may make low > high, which is infeasible, so don't proceed any further; or it make may make low == high, which may be of no interest whatsover, so you may want to test for that. The code below works correctly either way, because of the +1 and -1: generate a random number in {low..high}

int randomPart = r.nextInt(high-low+1)+low-1;

Then round it upwards to a multiple of 5. The prior shenanigans with low and high assure it is in range:

int nextInt = ((randomPart+4)/5)*5;
查看更多
劳资没心,怎么记你
5楼-- · 2020-03-27 06:00

Picks a random number between your values and then tests if it is divisible by div. If it is it returns that value otherwise it will have to do at max div-1 iterations to get to a number divisible by div.

In your situation call rBetweenGenerator(min, max, 5)

public int rBetweenGenerator(int min, int max, int div)
{
  int res = min + ((new Random()).nextInt(max - min + 1))
  for(int i = res; i < res + div; i++)
  {
    if( i % div == 0 )
    {
      return i;
    }
  } return -1; //error
}
查看更多
唯我独甜
6楼-- · 2020-03-27 06:03

This method first computes how many numbers divisible by 5 are in the given range. It picks a number between 0 and that count at random, and translates that random number back into the given range by multiplying it with 5 and adding it to the lower bound.

Note that both lowerBound and upperBound are inclusive.

public static int getRandomDivisibleByFive(int lowerBound, int upperBound) {
    if (lowerBound > 0) lowerBound += 4;
    if (upperBound < 0) upperBound -= 4;

    lowerBound /= 5;
    upperBound /= 5;

    int n = upperBound - lowerBound + 1;

    if (n < 1) {
        throw new IllegalArgumentException("Range too small");
    }

    return 5 * (lowerBound + new Random().nextInt(n));
}
查看更多
登录 后发表回答