954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Generating Numbers from 0.0 to 999 999

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

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 

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
stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 

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

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 
you cannot shuffle double numbers

Would you like to justify that somewhat surprising statement?

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
Would you like to justify that somewhat surprising statement?


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

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 
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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 

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

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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?

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 

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];
		}
	}
chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 
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).

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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[i] = amounts[r];
doesn't exactly swap the two values, although it is part of it.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Bump

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 
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[i] = 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];
	}
chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 
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?

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 

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.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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;
		}
	}
chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 

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.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
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

chiiqui
Junior Poster
107 posts since Sep 2011
Reputation Points: 10
Solved Threads: 3
 

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;
		}
	}
JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: