Results 1 to 7 of 7
- 10-22-2008, 01:55 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 25
- Rep Power
- 0
restrictions on randomly generated values?
Is the a command in Java where you can disallow certain numbers from the range to be obtained with the Random class?
So if you want to generate 5 random integers between 1-50, but you do not want any of them to be 15 or 20, for example, is there a way to block them or do you have to make some sort of loop where you get a new number if one of the disallowed ones is obtained?
-
I think a simple loop would be the way to go here.
- 10-22-2008, 02:44 PM #3
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
What Fubarable says, just use a while loop. (no for)
I die a little on the inside...
Every time I get shot.
- 10-22-2008, 06:33 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, use a while loop with check the appropriate condition you want to find.
- 10-22-2008, 06:57 PM #5
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
For your example, I would do as the others have suggested, and just have a loop which generates the number inside it and repeats until that number is neither 15 nor 20.
However, to take the general case, for me it would really depend on the circumstances; i.e. whether the code is part of an algorithm that has to be very fast, and what proportion of the range was excluded.
For example, if your range was 1-50, inclusive, but you wanted to exclude numbers in the range 5-44, then I would probably take a different approach. in this case, since there are only 10 permitted values then I would generate a random number between 1-10 and use a calculation to map this number onto the allowed values.
To take another example, suppose you wanted to exclude odd numbers, then I would simply generate a number between 1-25 and double it.
- 10-23-2008, 11:14 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
For the first suggestion; my question is how to map numbers into the allowed values. I'm not clear that how you going to do it.
For the second; I don't think it's a good choice actually. If you double the value, it's not a random value in sense, is it? Because you generating the second number, but effect with the initial value.
- 10-23-2008, 11:48 AM #7
Member
- Join Date
- Oct 2008
- Location
- UK
- Posts
- 65
- Rep Power
- 0
busdude was asking a general question, but using a specific example, as I said in this example a loop would be the best way, but I give two other examples where I think it would not.
In my first example, let me make it clear how you would do the mapping:
And what it does is this:Java Code:Random r = new Random(); int random = r.nextInt(10); int mapped = (random<=3) ? random+1 : random + 41; System.out.println(mapped);
random-->mapped
0-->1
1-->2
2-->3
3-->4
4-->45
5-->46
6-->47
7-->48
8-->49
9-->50
In my second example, I said that odd numbers have to be excluded.
And it maps like this:Java Code:Random r = new Random(); int random = r.nextInt(25); int mapped = (random+1)*2
random-->mapped
0-->2
1-->4
......
23-->48
24-->50
By definition, if any numbers are excluded then the distribution is not uniform.
Similar Threads
-
NoClassDefFoundError in Eclipse Plugin Dev: Are Access Restrictions responsible?
By SatishP in forum EclipseReplies: 1Last Post: 09-29-2008, 09:45 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
getting dynamically generated valus
By abhiN in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-29-2008, 10:58 AM -
passing dynamically generated values
By abhiN in forum Advanced JavaReplies: 1Last Post: 01-20-2008, 03:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks