-4

I want to generate a 100 unique random numbers, but the numbers need to be in the range of 1-10. Right now I am doing this:

for (int i = 0; i < 100; i++) {
    Double n = rand.nextDouble(10) + 1;
    arr[i] = n;
}

I could get Double numbers by checking if they're unique using if/else statements in arrays but it is very difficult and inefficient because the numbers(Doubles) could be almost infinite.

So how do i make sure the numbers are unique without using arrays?

Are there any data structures in java which do not allow duplicate elements?

H.Shirazi
  • 7
  • 1
  • 4
  • 3
    So what exactly is the problem? – Mureinik Nov 19 '17 at 17:35
  • Well, i am already generating a 100 random numbers(ints) between 1-10 but they need to be unique. Can i do it with nextDouble() instead of nextInt(). – H.Shirazi Nov 19 '17 at 17:38
  • 7
    That's impossible. You can't have 100 unique integers between 1 and 10. Only 10: 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10. So yes, you need to generate random double numbers. – JB Nizet Nov 19 '17 at 17:40
  • Well then call `nextFloat`/`nextDouble` instead. – Oliver Charlesworth Nov 19 '17 at 17:42
  • Just increment by `0.1` and randomly skip. Break your loop as soon as you got your 100 "unique" double values. – Filburt Nov 19 '17 at 17:43
  • Possible duplicate of [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – Suvam Roy Nov 19 '17 at 17:47
  • Possible, but i didn't get my answer there, nobody mentioned anything about Sets or Hashset to take care of uniqueness of numbers. – H.Shirazi Nov 19 '17 at 18:04

3 Answers3

5

As you alluded to in the comments, there's no way to generate 100 unique integers between 1 and 100. Using doubles allows you to do that, but does not guarantee uniqueness by itself (even though there's an infinitesimal chance of getting repeating items), so you would have to guarantee this yourself - e.g., by using a Set:

Set<Double> doubles = new HashSet<>();
while (doubles.size() < 100) {
    doubles.add(1 + rand.nextDouble() * 9);
}

EDIT:
JDK 8 provides an arguably more elegant way of achieving this with streams:

List<Double> doubles =
    rand.doubles()
        .distinct()
        .map(d -> 1 + d * 9)
        .limit(100)
        .boxed()
        .collect(Collectors.toList()); 
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    I cannot see in what way the second solution could be more elegant. Don't get me wrong, it's really good that you showed completely different approach! I just simply cannot see how that could've been better. It is slower, harder to read and the only positive aspect is that it fits into the single line, which the first solution is also capable of. Is there something I am missing? Regardless, huge +1! – Fureeish Nov 19 '17 at 17:50
0

You can use something like this.

import java.util.*;

public class MyClass {
    public static void main(String args[]) {
        double[] arr  = new double[100];
        for (int i = 0; i < 100; i++) {
            Random rand = new Random();
            double n = 10*rand.nextDouble();
            System.out.println(n);
            arr[i] = n;
        }
    }
}

But it is not possible to get unique 100 integers between 1-10.

The above program generate something like this

4.142037859604101
2.655156962359073
3.7397565068261205
2.9699181925544247
2.747362791293101
3.243100861992423
9.308481386292623
4.96298205308679
3.4319099852820822
9.951375372917328
3.6941158775736316
1.0388381644118727
1.6895078811799191
0.9166823110491484
9.60259797093202
2.3365812211691708
8.556399515524168
2.570971809286772
1.6621912919374215
0.5896588206170794
6.921688301938134
6.325470591144598
2.35492413118687
2.1778674294915454
Pardha.Saradhi
  • 468
  • 1
  • 10
  • 27
0

The class Math has a bunch of useful functions, also at least one which you could use for this.

for(int i = 0; i < 100; i++) {
        double n = (Math.random()*9) + 1;
        arr[i] = n;
}

Math.random returns a random positive greater than or equal to 0.0 and less than 1.0, so if you multiply that by 9, you get a number greater than or equal to 0.0 and less than 9.0. And then the plus 1 is to make sure the range is between 1 - 10.

If you really want to make sure that all numbers are unique you could write a method that checks if the number is not already in the array.

Hopefully this helps you out. Good luck!

  • `Math.random()`s return value is in the range of `[0, 1)`, not `(0, 1)`. It **could** be equal to `0`. Just to not confuse anyone – Fureeish Nov 19 '17 at 17:52