-3
public static void main(String[] args) {
    for(int i = 0; i < 10000; i++) {
        long number = (long) Math.floor(Math.random() * 90000000) + 100000000L;
        System.out.println(number);
    }
}

The above code doesn't generate 10,000 random numbers. Why?

Felix Glas
  • 15,065
  • 7
  • 53
  • 82

5 Answers5

2

If you don't require uniform distribution, you could start with 100,000,000 and add an increment randomly chosen between [1 and 89,999]. Repeat 10,000 times.

This will give you a non-repeating sequence of numbers that are somewhat randomly generated.

But they will not be uniformly distributed (it is very unlikely to get numbers in the high ranges, like 999,xxx,xxx), and they will be strictly ascending, so you have a good chance to guess the next one if you know the previous one ("good" = 1/89999).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

Do it this way instead:

public static void main(String[] args) {
    Set<Integer> uniques = new HashSet<>(15000);
    Random random = new Random();
    for (int i = 0; i < 10000;) {
        int number = random.nextInt(Integer.MAX_VALUE);
        if (uniques.add(number)) {
            System.out.println(i + ": " + number);
            i++;
        }
    }
}
Harmlezz
  • 7,972
  • 27
  • 35
0

Using Set, you can ensure uniqueness. Try,

 Set<Long> resSet=new HashSet<>();
 for(int i = 0;  resSet.size()<10000; i++){
     long number = (long) Math.floor(Math.random() * 90000000) + 100000000L;
     resSet.add(number);
 }
 System.out.println(resSet);
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

I had a similar problem and I did the following, generate random number and check it has been generated before, if not, just use and note it.

List<String> gen = new ArrayList<String>();
long number=0;
int i=0;

while(i<10000)
{
   while(!gen.contains(number) || i==0)
      number = (long) Math.floor(Math.random() * 90000000) + 100000000L;
   gen.add(number);
   System.out.println(number);
   i++;
}

I didn't tested the code since I did it in .NET, but I searched for code equivalencies.

Hope it helps!

рüффп
  • 5,172
  • 34
  • 67
  • 113
ZeroWorks
  • 1,618
  • 1
  • 18
  • 22
  • i did try this and i got 10,000 random numbers,but these numbers are of 8 and 9-digit values(characters).....i want only 9 digit values – user3651409 May 21 '14 at 10:38
  • I used your algorithm to get number... so try this instead... replace: number = (long) (Math.floor(Math.random() * 999999999D) + 100000000L); Adding 100000000 to random should return a 9-digit value. – ZeroWorks May 21 '14 at 12:10
0

A minor contribution to @Masud's code:

         Set<Long> resSet = new HashSet<Long>();
         while(true) {
             long number = (long) Math.floor(Math.random() * 90000000) + 100000000L;
             resSet.add(number);
             if(resSet.size() == 10000)
                 break;
         }

This way you can surely get 10000 unique numbers (since the for loop does not guarantee that).

One drawback of this approach is that it "perishes" several numbers (which are the same as previous ones). I don't know if there is some more clever approach to this.

Eypros
  • 5,370
  • 6
  • 42
  • 75