2
We need to generate random numbers within a certain digit range with few restrictions 
e.g. For double digit range 11 - 99, the resultant output should not include all like numbers [11,22,33,44,...99] and multiples of 10 [20,30,40....90]

The resultant output should be [12,13,14,15,16,17,18,19,21,23,...98]

Note: This function should work seamlessly for other digit ranges also (e.g. 3 digit ranges spanning 101 - 999 and four digit ranges spanning 1001 - 9999)

We are having difficulties in identifying like numbers (e.g 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, ...., 3333 ...)

EDIT1:

protected static List<Integer> fetchRandIntegers(int min, int max, int howMany, boolean randomize) {

    // We need to reverse minimum, maximum values for negative ranges
    if (min > max) {
        int tmp = min;
        min = max;
        max = tmp;
    }

    List<Integer> allNumbers = new ArrayList<Integer>();
    for (int i = min; i <= max; i++) {
        allNumbers.add(i);
    }

    if (randomize) {
    ...
    }

    return allNumbers;

}

Joe
  • 14,513
  • 28
  • 82
  • 144
  • Sounds like this person has the same homework http://stackoverflow.com/questions/6620840/is-there-any-goto-statement-in-java – Peter Lawrey Jul 08 '11 at 09:12
  • @Peter - I am here to solve genuine problems which I have during the coding process. It's just irritating to see some insane comments, which has nothing to do with the question. I guess I am just wasting my time trying to explain. – Joe Jul 08 '11 at 16:27
  • if you actually **read** the post you would see there are many answers to the same problem. I find it insane that people can't be bothered to read answers from people trying to help them. BTW I find it an amazing co-incidence that someone else would post a question on such a unique problem within an hour of yours. In any case I have given my answer already once today and it covers your whole requirement, not just part of it and doesn't use a loop so is likely to be faster. – Peter Lawrey Jul 08 '11 at 19:48
  • @peter perhaps it is related, but it's hard to tell since one is formulated as "help me translate this crazy c++ code to java with gotos" and this is about random number generation. – Jeff Atwood Jul 11 '11 at 10:45
  • @Jeff, and yet the sequence of numbers is exactly the same. ;) – Peter Lawrey Jul 11 '11 at 11:08

4 Answers4

3

Two simple options:

  • Generate any random number in the range, and do it again (and again...) if you pick a "banned" one
  • Work out how many eligible numbers you actually have, generate a number in the range [0..size) and then map that onto an eligible number

The latter is potentially more efficient (you don't loop around generating numbers which are then discarded), but more complicated to implement.

EDIT: Here's a method to check whether all the digits in an integer are the same:

public boolean checkForAllOneDigit(int value)
{
    int digit = value % 10;
    value = value / 10;
    while (value > 0)
    {
        if (value % 10 != digit)
        {
            return false;
        }
        value = value / 10;
    }
    return true;
}

(There may be a slightly more elegant way of writing the loop, but I haven't had coffee yet... the "modulo 11 or 111 or 1111 etc" approach is really neat too.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • We actually create a subset of numbers from this randomly generated list and it's a requirement for us. – Joe Jul 08 '11 at 05:31
  • We are having difficulties in identifying like numbers (e.g 11, 22, 111, 3333 ...). Otherwise we are fine. – Joe Jul 08 '11 at 05:31
  • 1
    @Joe: It's not clear what your first comment means. As for identifying numbers with the same digits - you *could* take the `value % 10`, and repeatedly divide by 10 and take the `value % 10` until either you've gone through all the digits (it's of the form 1111, 2222 etc) or you get a different remainder (in which case it's okay). See my edit for some sample code. – Jon Skeet Jul 08 '11 at 05:57
3

To identify if integer i has all digits the same:

  • Convert i to string and compare the characters, or
  • Repeatedly modulo and divide by 10 and check if all modulos are the same

with something like:

public boolean hasAllDigitsSame (int i)
{
    int a = i ;
    int m = a % 10 ;
    int mm = m ;
    while(a > 0)
    {
        m = a % 10;
        if (m != mm)
          return False ;
        a /= 10 ;
    }
    return True ;
 }

To identify if integer i is a multiple of 10 (100, 1000):

  • Check if i modulo 10 is 0.
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
  • Is there a simpler mechanism to find if integer has similar digits – Joe Jul 08 '11 at 05:45
  • 3
    You could create the ones version of the number with a loop and then see if the number modulo the ones is zero. If so, it is an invalid number. For example, `34 % 11 = 1`, so 34 is not a repeating number. `555 % 111 = 0`, so 555 is a repeating number. – 101100 Jul 08 '11 at 05:49
  • @101100: This is very clever, indeed. – ypercubeᵀᴹ Jul 08 '11 at 05:58
2

This program will also give the answer

import java.util.*;  

public class Generate {  

      public static void main(String[] args){  

   Scanner scan = new Scanner(System.in);  
   System.out.println("\nEnter the limit:");  
   int k = scan.nextInt();  
   int i,j,w,l,q,d;  
   for(i=13;i<=k;i++)
   {  

   j=i%10;  
   if(j!=0)  
   {  
       if(i<99)  
        {  
          for(w=1;w<=9;w++)  
               {  
                   l=11*w;  

                   if(l==i)  
                      {  

                      i++;  
                      continue ;  
                       }  
                }  
          }  
    if(i>99)  
         {  
            for(q=1;q<=9;q++)  
              {  
                 d=111*q;  

                  if(d==i)  
                      {  

                      i++;  
                      continue ;  
                      }  
              }  
         }  
      System.out.println(i);  

    }  
   }  

}  
}  

I know this program is quite big, just to give an idea ,i have given this.But i am sure it will give correct answer !!

jeb
  • 78,592
  • 17
  • 171
  • 225
Pari Sairam Mohan
  • 421
  • 1
  • 4
  • 11
0

how big a lookup table are you willing to use? (Map consecutive range to the bigger range that doesn't have your forbidden values.)

Or do your check (if under 100, multiples of 10 and 11, if under 1000 multiples of 111 and 100, and so on), and if the fish is too small throw it back?

david van brink
  • 3,604
  • 1
  • 22
  • 17
  • Its any multiple of 10 and like numbers is what I want to exclude and its nothing to do with the range – Joe Jul 08 '11 at 05:37