0

I have this array

int [] marc = new int[4];

i need to insert a set of non repeating random numbers in the range of 1-10 to it

i'm using this for loop to set random numbers

for (he = 0; he < 4; he++) {
    marc[he] = rn.nextInt(10 - 1 + 1) + 1;
    marc[he]++; 
}

it gives me random numbers but repeated ones inside the array

i'm also using

java.util.Random;
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122

2 Answers2

1

Using java.util.Random; will generate repetitive numbers more often when your range is small, which in your case is only 10. There is no condition in your code that checks whether the generated random number already exists in your array or not.

Before inserting a generated number in to the array, you first need to check whether that number already exists in your array. If it does not, you insert that number in the array, otherwise you generate a next random number.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
1

Well, yes, numbers can be repeated while being random. You need to do your own logic to validate if they're already on the array, this can be done with the following code:

In the code I used an array of 10 elements to observe there aren't repeated numbers even on that situation.

import java.util.Random;

public class RandomNumbersNoRepeating {
    public static void main(String[] args) {
        int array[] = new int[10];
        Random random = new Random();

        //Fills the array
        for (int i = 0; i < array.length; i++) {
            boolean found = false;
            int r = 0;
            do {
                found = false;
                r = random.nextInt(10) + 1;
                //Here we check if the number is not on the array yet
                for (int j = 0; j < array.length; j++) {
                    if (array[j] == r) {
                        found = true;
                        break;
                    }
                }
            } while (found);

            array[i] = r;
        }

        //Prints the array
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}

Another possible solution, as given in a comment could be to shuffle an array from 1-10, and get the first four numbers

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • How could i shuffle the array?? – BlazePage001 Jan 09 '17 at 19:13
  • That's another question, the question here is "how to generate an array of 4 random numbers without repeating them", if you want to know how to shuffle an array you can [Google it](https://www.google.com.mx/search?client=ubuntu&channel=fs&q=java+shuffle+array&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=H-FzWODlDufZ8gef-4CgCQ) and the very first result leads you to: [this](http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) if you don't understand it, then please ask a question explaining what you don't understand and why and include a valid [mcve] – Frakcool Jan 09 '17 at 19:15
  • If the array was large, using a `Set` to keep track of the already used values would be a little faster. – Andrew S Jan 09 '17 at 21:54