How do you generate a random integer in a specified range, divisible by 5? [closed]

假装没事ソ 提交于 2019-12-30 10:59:09

问题


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

I'm using Java


回答1:


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




回答2:


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;



回答3:


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;



回答4:


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));
}



回答5:


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
}


来源:https://stackoverflow.com/questions/19341074/how-do-you-generate-a-random-integer-in-a-specified-range-divisible-by-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!