0

I want to generate random integers from range 0-9 and put it in an array of size 100. That's easy but I'm clueless on how to make sure that in the array, I have at least one occurrence of every integer in the range 0-9.

This is all using java by the way.

This is what I've got so far (the numbers are different in my coding because I wanted to ask a simpler question):

public static int[] extendTo1024(int[] key) {
    int[] extendedKey = new int[1024];
    Random random = new Random();
    for(int i = 0; i < 1024; i++) {
        int rand = random.nextInt(64) + 1;
        extendedKey[i] = bitKey[rand];
    }
    return extendedKey;
}

Any help? Thank you in advance!

Tony
  • 219
  • 4
  • 17
  • 1
    First please show what you have done to achieve this! – Arun Sharma Sep 13 '14 at 04:11
  • 3
    Just add 10 items from 0 to 9, generate the remaining 90 and then shuffle the array. – Jack Sep 13 '14 at 04:15
  • 1
    If the integers in the array were generated randomly there would always be a small but non-zero chance that at least one of the integers in the range was not present in the array. Therefore, do I understand correctly that you are looking for a way to generate integers that is not actually random? If so, it would be helpful if you could describe the required characteristics of this non-random sequence of integers in the range 0 to 9. – Simon Sep 13 '14 at 04:17
  • @Arun I have edited to reflect this! – Tony Sep 13 '14 at 04:18
  • @Simon It's not truly random, you're correct. The characteristics for my array should be this - 1. Array must contain all numbers in the range and 2. the numbers put into the array are in randomized order. Hope that helps you understand it a bit more. – Tony Sep 13 '14 at 04:21
  • You're saying you have an array of size 100 and numbers from 0-9, but in the source code you posted you have an array of size 1024 and numbers 1 to 64. That doesn't add up. Please update your question or your code. – Erwin Bolwidt Sep 13 '14 at 04:22
  • @ErwinBolwidt Does it really matter? The question can be generalized to a set of K symbols and an array of N elements where K < N without any change in meaning nor solution. – SnakE Sep 13 '14 at 04:24
  • StackOverflow is about high quality questions and answers that will be useful for people who have a similar problem. Having code and a question that don't match is not a good sign IMHO. – Erwin Bolwidt Sep 13 '14 at 04:25

2 Answers2

3
  1. Fill the first 10 elements with numbers 0-9
  2. Fill the rest with random numbers
  3. Shuffle the array
SnakE
  • 2,355
  • 23
  • 31
  • @ErwinBolwidt Actually Jack beat me by 3 minutes. But for some reason he commented on the OP instead of answering. – SnakE Sep 13 '14 at 04:26
  • Thanks, but as I said in the other post, I want the first 10 cells of the array to be random also meaning that something like this could happen in the first 10 cells {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}. – Tony Sep 13 '14 at 04:27
  • @Tony, that's why there's step 3 for that. – lxcky Sep 13 '14 at 04:27
  • Oh okay I understand it! Thank you :) so I should write a method that shuffles the array after its been filled? – Tony Sep 13 '14 at 04:29
  • 1
    @Tony: See the answer to [Random shuffling of an array](http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) – Simon Sep 13 '14 at 04:32
1
ArrayList<Integer> al = new ArrayList<Integer>();

//make sure the array contains all occurences of 0-9
for (int i = 0; i < 10; i++) {
    al.add(i, i);
}

//generate random number for the remaining 90
for (int i = 10; i < 100; i++) {
    int random = (int) (Math.random() * 10);            
    al.add(i, random);
}

//shuffle the random numbers to make sure that the first 10 are randomly placed
Collections.shuffle(al);

//Convert it back to array (In case you need it to be array not ArrayList)
Integer[] randomNums = al.toArray(new Integer[100]);

//result
for (int i : randomNums) {
    System.out.println(i);
}
lxcky
  • 1,668
  • 2
  • 13
  • 26
  • Thanks for trying, I really appreciate it :) but I want the first 10 arrays to also be randomly generated as well alongside with the other 90. – Tony Sep 13 '14 at 04:25