5 Replies - 4944 Views - Last Post: 02 August 2015 - 12:49 PM Rate Topic: -----

#1 joyang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 15

Generate random numbers without duplicates

Posted 02 August 2015 - 04:15 AM

Does anyone knows how to generate random numbers without duplicate with Math.random. Any help is appreciated.


int minMedical = 0;
int maxMedical = 9;
int randNum1 = minMedical + (int) (Math.random() * (maxMedical - minMedical) + 1);
int minMedicalY = 0;
int maxMedicalY = 9;
int randNum2 = minMedicalY + (int) (Math.random() * (maxMedicalY - minMedicalY) + 1);

Is This A Good Question/Topic? 0
  • +

Replies To: Generate random numbers without duplicates

#2 Inchidi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 21-July 15

Re: Generate random numbers without duplicates

Posted 02 August 2015 - 05:54 AM

how many random int's you need?
if you need 2 random integers then

int int1st = 0; // this is 1st int we need
int int2nd = 0; // this is 2nd int we need
int min = 0; // this is min value, random may generate 0 but not -1
int max = 9; // this is max value, random may generate 9 but not 10
while(int1st == int2nd) {
    Random rand = new Random(); //don't forget to import java.ForgotThePackName.Random;
    int1st = rand.nextInt((max - min) + 1) + min; 
    int2nd = rand.nextInt((max - min) + 1) + min; // if you need more than 2 int then make it
}


Was This Post Helpful? 0
  • +
  • -

#3 joyang   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 15

Re: Generate random numbers without duplicates

Posted 02 August 2015 - 06:43 AM

View PostInchidi, on 02 August 2015 - 05:54 AM, said:

how many random int's you need?
if you need 2 random integers then

int int1st = 0; // this is 1st int we need
int int2nd = 0; // this is 2nd int we need
int min = 0; // this is min value, random may generate 0 but not -1
int max = 9; // this is max value, random may generate 9 but not 10
while(int1st == int2nd) {
    Random rand = new Random(); //don't forget to import java.ForgotThePackName.Random;
    int1st = rand.nextInt((max - min) + 1) + min; 
    int2nd = rand.nextInt((max - min) + 1) + min; // if you need more than 2 int then make it
}


I would like to create a 2D array of 10 by 10, and then generate 4x 15 random numbers using a normal array for x coordinate an y coordinate without duplicates in it and lastly fill up the empty spaces with a char 'E", do you have any idea?
for (int foodX[] = new int[15];
        int foodY[] = new int[15];
for (int i = 1; i < foodX.length + 1; i++) {
            int minFood = 0;
            int maxFood = 9;
            int randNum1 = minFood + (int) (Math.random() * (maxFood - minFood) + 1);
            int minFoodY = 0;
            int maxFoodY = 9;
            int randNum2 = minFoodY + (int) (Math.random() * (maxFoodY - minFoodY) + 1);
            values[randNum1][randNum2]=foodName;

        }

int i = 0; i < values.length; i++) {

	    // Loop and display sub-arrays.
	    char[] sub =  values[i];
            
	    for (int x = 0; x < sub.length; x++) {
		System.out.print(sub[x] + " ");
	    }
	    System.out.println();
            
            
	}


This post has been edited by baavgai: 02 August 2015 - 10:31 AM
Reason for edit:: tag fix

Was This Post Helpful? 0
  • +
  • -

#4 Inchidi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 21-July 15

Re: Generate random numbers without duplicates

Posted 02 August 2015 - 07:43 AM

on my example i am checking int1st and int2nd values
if you need more random int
then you can check that too, change line 5 from my example with something like this
while( Arrays.asList(yourArray).contains(int1st) ) {


and set int1st with new random int to get new random with different value in while body
then fill yourArray with int1st after while done
this is not a good idea with small random distance and make sure your random distance is greater than your array~
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Generate random numbers without duplicates

Posted 02 August 2015 - 10:41 AM

If I'm following, you want to choose 15 random positions (x,y) rather than just 15 random numbers without duplicate. You want those 15 positions to be in the scope of a 2D array.

That actually makes it easier, believe it or not. The steps are:
  • Initialize your 2D array to all empty values.
  • Pick a random position in the array.
  • If the position is empty, add the value you want and increment your item counter.
  • If the position is not empty you must try again.
  • If your counter still needs more incrementing, got back to step 2.


Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 salazar   User is offline

  • D.I.C Addict

Reputation: 111
  • View blog
  • Posts: 664
  • Joined: 26-June 13

Re: Generate random numbers without duplicates

Posted 02 August 2015 - 12:49 PM

I would like to recommend using the java.awt.Point class. It will make it easier to compare the points. If you've never heard of it, you should look into it. It's good OO practice.

This post has been edited by salazar: 02 August 2015 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1