3

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

I'm using Java

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
cbaird1911
  • 49
  • 1
  • 5
  • 7
    Have you tried anything? – nanofarad Oct 13 '13 at 02:01
  • I can generate a random number using the following: random.nextInt(max - min) + min; However, I don't know how to make the randomly generated integer divisible by 5. – cbaird1911 Oct 13 '13 at 02:02
  • 6
    Have you thought about division and multiplication? Or did you choose to ask here instead? – nanofarad Oct 13 '13 at 02:03
  • 1
    So if the range is [3,7] then the only number you can generate is 5? – stevemarvell Oct 13 '13 at 02:14
  • 2
    If the range is [1,4], what should your program do? – John Dvorak Oct 13 '13 at 02:16
  • Make use of modulus operator or multiplying the answer by 5 otherwise do more reading b3fore u ask plz – Manny265 Oct 13 '13 at 18:11
  • another good question lost to the closing power-trip. obviously the question as worded by the OP technically fits the bill, but if the power-trippers were to either (a) look at the attempts by members to answer; or (b) tried to answer it themselves, they would realize that several interested members who have more than a minimal understanding have attempted solutions, discussed why they are not working. if the close voters had any concern for the community one of them would simply edit or request an edit of the question. but closing gives them the satisfaction of pissing on the effort below. – necromancer Oct 14 '13 at 02:46

5 Answers5

6

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

Community
  • 1
  • 1
necromancer
  • 23,916
  • 22
  • 68
  • 115
1

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;
stevemarvell
  • 981
  • 1
  • 6
  • 16
1

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;
user207421
  • 305,947
  • 44
  • 307
  • 483
0

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));
}
Thomas
  • 17,016
  • 4
  • 46
  • 70
-3

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
}
Isaac
  • 310
  • 1
  • 6