-2

I have looked up how to add 0-99 to an array. But, my assignment was 1-1000000. I just keep getting really large numbers and no small numbers. Is it just because the chance of getting large numbers is a lot higher? I just wanted to make sure I was doing it right. Thanks in advance for any help!

int a[]= new int[50];
for(int i = 0; i < 50; i++) {
    a[i] = (int)(Math.random() * 10000000);
}
bsd
  • 2,707
  • 1
  • 17
  • 24
MostPalone
  • 77
  • 7
  • depends what you mean by 'small' – pvg Feb 09 '17 at 03:09
  • What do you consider to be a large number? – PM 77-1 Feb 09 '17 at 03:10
  • 1
    This is the normal case. If the numbers all have equal distribution, the probability of getting a number > 1000000 is 0.9, while a number <= 1000000 is 0.1. Going one step further, a number <= 100000 has a probability of 0.01. And that's still a pretty large number. A number below 1000 has a probability of 1/10000. I've just noticed you use 1mio as upper-bound in the question itself, but 10mio in the code. My values are based on an upper bound of 10mio. –  Feb 09 '17 at 03:12
  • I mean, there are never any 1-5 digit numbers. Just numbers with 6+ digits. I might just acting stupid, because there are so many more 6+ digits numbers. I just wanted to make sure my code was right. Thanks! – MostPalone Feb 09 '17 at 03:13
  • 1
    Are you asking how to bias results in favor of smaller numbers, or are you just confirming your code is correct? – shmosel Feb 09 '17 at 03:16

4 Answers4

0

Well if you hoped to get a number below 1000, there's a 1/10000 chance, which translates to 0.01%. Your code is fine, but with these settings getting a low number is quite unlikely

Cedric Martens
  • 1,139
  • 10
  • 23
0

Depends what do you call by small numbers.

For example, getting number lower than 1000 means that it would need to fit to lower 0.1% of the interval as 1000 it's just 0.1% of all the numbers from 1 million.

Additionally, note that this way you'll get numbers between 0 and 999999 (inclusive). To get 1-1000000 you need to add 1:

a[i] = (int)(Math.random() * 10000000)+1;
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
0

Yes, large numbers will dominate with that. That's because half the numbers are over 500,000. Similarly, 9/10 of the numbers are over 100,000. So 9/10 of the numbers will have six digits.

Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
0

[Somewhat orthogonal solution to your question]

One of the options you can consider is to shuffle the array after adding the numbers based on your expected distribution, which can be all numbers added once/certain numbers added multiple times/certain prime numbers multiple times, etc.

I personally used it to cover some cases for a game development.

Here is an answer which can help you shuffle: Credits

 List<Integer> solution = new ArrayList<>();
    for (int i = 1; i <= 6; i++) {
        solution.add(i);
    }
 Collections.shuffle(solution);
Community
  • 1
  • 1
vishwarajanand
  • 1,021
  • 13
  • 23