is there anyway to generate numbers from 0.0 to 999 999 without repetition? I am thinking of making an array that you can put 27 integer datas in it like this

double amounts[] = { 0, 0.01, 1000000, 25, 250000, 75, 50, 1000,
			200, 100, 400000, 750, 5000, 750000, 500, 100000, 300, 75000, 800,
			20, 300000, 10, 50, 750, 25, 5, 1 };

and then after that I'll assign this values to a setter and getters. and these numbers shouldn't repeat, and I want to shuffle their indexes, for example, amounts[1] contains 0.01, I want to randomize their arrangement like for example, amount[1]will contain 25, and so on and so forth sorry I have a bad english

You want to fill the 27 indices of the array with random values between 0 and 999 999?

In that case you can create an ArrayList of size 27 and use Random class to generate the number for you. You can use "contains" method of ArrayList to check if the number is already in your list. Since ur list size is just 27 it should be that costly.

This way you can randomly generate stuff.....

Read here...

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Random.html

You want to fill the 27 indices of the array with random values between 0 and 999 999?

In that case you can create an ArrayList of size 27 and use Random class to generate the number for you. You can use "contains" method of ArrayList to check if the number is already in your list. Since ur list size is just 27 it should be that costly.

This way you can randomly generate stuff.....

Read here...

http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Random.html

you cannot shuffle double numbers

you cannot shuffle double numbers

Would you like to justify that somewhat surprising statement?

Would you like to justify that somewhat surprising statement?

Tried it, and I made a research about it, you cannot shuffle double values

Create an array of doubles.
loop quite a few times 
  generate two random ints 0 to (array.length-1)
  swap the array elements at those two indexes

result: a shuffled array of doubles.
What's so hard?

ps Or use Collections.shuffle with your doubles wrapped in Doubles

Create an array of doubles.
loop quite a few times 
  generate two random ints 0 to (array.length-1)
  swap the array elements at those two indexes

result: a shuffled array of doubles.
What's so hard?

ps Or use Collections.shuffle with your doubles wrapped in Doubles

didn't saw that coming, sorry. gonna test some more with my code thanks

In that case, here's a slightly harder to understand, but much faster algorithm:

for i = array.length-1; i>=1; i--
   get random int r = 0 to i
   swap element i with element r

(this is the algorithm used in Collections.shuffle)

This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive. This method runs in linear time

Source: JavaDoc for Collections

In that case, here's a slightly harder to understand, but much faster algorithm:

for i = array.length-1; i>=1; i--
   get random int r = 0 to i
   swap element i with element r

(this is the algorithm used in Collections.shuffle)

Source: JavaDoc for Collections

what does get random int r = 0? how do I do that?

In that case, here's a slightly harder to understand, but much faster algorithm:

for i = array.length-1; i>=1; i--
   get random int r = 0 to i
   swap element i with element r

(this is the algorithm used in Collections.shuffle)

Source: JavaDoc for Collections

here's what I did

public void Shuffle(){
		
	Random rand = new Random(26);
		
		for(int i = amounts.length-1 ;i>=1;i--) {
		    int r = rand.nextInt();
		    amounts[i] = amounts[r];
		}
	}

what does get random int r = 0? how do I do that?

that was "r = 0 to i" - a random int >=0 and <= i

Have a look the java.util.Random class and its nextInt(int n) method. (clue - it does exactly what you need).

Getting close... you need the version of nextInt that lets you specify an upper bound for the random number. Maybe that's what you thought the 26 on line 3 does? - sorry it doesn't do that.
and
amounts = amounts[r];
doesn't exactly swap the two values, although it is part of it.

Bump

Getting close... you need the version of nextInt that lets you specify an upper bound for the random number. Maybe that's what you thought the 26 on line 3 does? - sorry it doesn't do that.
and
amounts = amounts[r];
doesn't exactly swap the two values, although it is part of it.

okay here's my 2nd attempt

public void Shuffle(){
		
	Random rand = new Random(26);
	int i = 0;
	int r = 0;
	for (i = amounts.length-1; i>=1; i--)
	   r = rand.nextInt();
	   r = i;
	   amounts[i] = amounts[r];
	}
public void Shuffle(){
		
		Random rgen = new Random();
		for (int i=0; i > amounts.length; i++) {
			int randomPosition = rgen.nextInt(amounts.length);
			double temp = amounts[i];
			amounts[i] = amounts[randomPosition];
			amounts[randomPosition] = temp;
	}
	}

how aobut this?

Getting closer and closer... although I have no idea why you changed the for loop to go in the wrong direction when you had that right the first time.
line 5 - the upper bound for the random number isn't what I said in the pseudocode

Just take a minute to put the pseudocode and your code side by side and compare every detail. Computer programming requires that every tiny detail is right.

ps I'm not saying that your code won't work. Your own testing will tell you that. It looks like a less efficient version of the algorithm I showed you, and looks like it will work, but I don't have time now to validate that.

Getting closer and closer... although I have no idea why you changed the for loop to go in the wrong direction when you had that right the first time.
line 5 - the upper bound for the random number isn't what I said in the pseudocode

Just take a minute to put the pseudocode and your code side by side and compare every detail. Computer programming requires that every tiny detail is right.

ps I'm not saying that your code won't work. Your own testing will tell you that. It looks like a less efficient version of the algorithm I showed you, and looks like it will work, but I don't have time now to validate that.

Here it is :D if you can rate it for me

public void Shuffle() {

		Random rgen = new Random();
		for (int i = 0; i < amounts.length - 1; i++) {
			int randomPosition = rgen.nextInt(amounts.length);
			double temp = amounts[i];
			amounts[i] = amounts[randomPosition];
			amounts[randomPosition] = temp;
		}
	}

That's not exactly the algorithm that I discussed before, but my guess is that it will work OK anyway. On the other hand, my opinion is not important; either it gives the right result or it doesn't. Certainly it's clean code without any obvious flaws. So run some tests. Shuffle your array a few times and print them all out and see if it looks randomised enough for you.

That's not exactly the algorithm that I discussed before, but my guess is that it will work OK anyway. On the other hand, my opinion is not important; either it gives the right result or it doesn't. Certainly it's clean code without any obvious flaws. So run some tests. Shuffle your array a few times and print them all out and see if it looks randomised enough for you.

uhmm it's working but if you wouldn't can you show me the full application your algorithm? just to get an idea of how you did it, if I mean full application not only the pseudocode :D

OK, just for fun - I haven't checked this for typos, but...
This has the algorithm as previously defined - starts at the top end and works down
swaps with elements lower than the current one.
I also pass in the array as a parameter to make this generally useful, rather than being tied to one app. Finally it's static because it doesn't depend on any particular instance (because the array is a parameter)

public static void Shuffle(float[] data) {

		Random rand = new Random();

		for (int i = data.length; i >= 1; i--) {
			int r= rand.nextInt(i+1);
			double temp = amounts[i];
			amounts[i] = amounts[r];
			amounts[r] = temp;
		}
	}

OK, just for fun - I haven't checked this for typos, but...
This has the algorithm as previously defined - starts at the top end and works down
swaps with elements lower than the current one.
I also pass in the array as a parameter to make this generally useful, rather than being tied to one app. Finally it's static because it doesn't depend on any particular instance (because the array is a parameter)

public static void Shuffle(float[] data) {

		Random rand = new Random();

		for (int i = data.length; i >= 1; i--) {
			int r= rand.nextInt(i+1);
			double temp = amounts[i];
			amounts[i] = amounts[r];
			amounts[r] = temp;
		}
	}

Where did you get this algorithm?

OK, just for fun - I haven't checked this for typos, but...
This has the algorithm as previously defined - starts at the top end and works down
swaps with elements lower than the current one.
I also pass in the array as a parameter to make this generally useful, rather than being tied to one app. Finally it's static because it doesn't depend on any particular instance (because the array is a parameter)

public static void Shuffle(float[] data) {

		Random rand = new Random();

		for (int i = data.length; i >= 1; i--) {
			int r= rand.nextInt(i+1);
			double temp = amounts[i];
			amounts[i] = amounts[r];
			amounts[r] = temp;
		}
	}

what does this part specifically do?

amounts[i] = amounts[r];

does it switch the indexes? or the values ? kinda confused on that part

Where did you get this algorithm?

I told you to re-read the previous posts. I already answered that.

what does this part specifically do?

amounts[i] = amounts[r];

does it switch the indexes? or the values ? kinda confused on that part

It does exactly the same as your

amounts[i] = amounts[randomPosition];

It takes the value is stored in the array element amounts[randomPosition] and copies that value into the array element amounts. So now both array elements contain the same value.

I said I didn't check for typo's - well, I should have checked more carefully! I forgot to change some of the array names to match the parameter I added. Here's that fixed. Sorry.

public static void Shuffle(float[] data) {
     
      Random rand = new Random();
     
      for (int i = data.length; i >= 1; i--) {
        int r= rand.nextInt(i+1);
        double temp = data[i];
        data[i] = data[r];
        data[r] = temp;
      }
    }

OK, the embarasment of this is a punishment for me breaking one of my own rules: "don't post code you haven't checked" THIS is the correct version

public static void shuffle(double[] data) {

      java.util.Random rand = new java.util.Random();

      for (int i = data.length - 1; i >= 1; i--) {
         int r = rand.nextInt(i + 1);
         double temp = data[i];
         data[i] = data[r];
         data[r] = temp;
      }
   }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.