Question

I'm trying to get random numbers 0 - 499 in sets of 2 based on what the user inputs. For example 234 and 58, and thats one set, but the user may prompt that they wan 8 sets. Im trying to make sure that a redundant number doesnt show up, like 234 & 58, 12 & 444, 198 & 58. (58 showed up twice.)

The way im trying to prevent this is by putting the found numbers into an array and when i go around the next match im checking in the array to make sure they havent been used yet. Was just wondering what the best way of this would be. Like obviously on the first go around no numbers have been chosen yet so i dont need to check. but then the next go around what if i get a redundant? i will get numbers and then check the array, and if it is already in the array how do i go back and get new numbers? a do while loop maybe?

here is what im doing:

//now add the specified number of random connections
    System.out.println();
    System.out.println("new connection(s): ");

    //array for keeping track of the new connections to prevent redundant add's
    int redundant [] = new int[con*2];

    for( int i = 1; i <= con; i++ ){
        Random ran = new Random();
        if( i == 1){
            int ran1 = ran.nextInt(sWorld.length-1) + 1;
            int ran2 = ran.nextInt(sWorld.length-1) + 1;
            redundant[i - 1] = ran1;
            redundant[i] = ran2;
            System.out.println("     " + ran1 + " - " + ran2);
        }
        else{
            int ran1 = ran.nextInt(sWorld.length-1) + 1;
            int ran2 = ran.nextInt(sWorld.length-1) + 1;
            //need help
        }

thanks in advance!

EDIT. Going with a maethod below (using collections)

        List<Integer> nums = new LinkedList<Integer>();
    for( int i = 0; i <= 499; i++ ){ 
        nums.add(i);
    }

    //shuffle the collection for more randomness


    System.out.println();
    System.out.println("new connection(s): ");
    for (int x = 1; x <= con; x++){
        Collections.shuffle(nums);

        Random ran = new Random();
        int r1 = nums.remove(ran.nextInt(nums));
        int r2 = nums.remove(ran.nextInt(nums));

but having trouble getting the random numbers, any help?

Was it helpful?

Solution 2

One way is to create a list of number from 0-499

List<Integer> nums = new LinkedList<Integer>();
for(int i=0; i<=499; i++) nums.add(i);

Then shuffle the the list

Collections.shuffle(nums);

Then each time you need a non-recurring random number between 0-499, just remove an element from the list

int x = nums.remove()

OTHER TIPS

Just fill a collection with all indices in the desired range (i.e. 0 to 499), and then use Collections.shuffle().

Use a while cycle and use an array. While your index is not out of scope generate two numbers. Add them to the array if they are not redundant and increase your index accordingly. If they are redundant, do nothing, the next iteration of the while cycle will generate two new numbers anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top