Results 1 to 8 of 8
Thread: Random Numbers from 1 onwards
- 12-18-2008, 01:50 AM #1
-
I've always found that adding 1 to the result works well for me.
i.e., if I want a random int from 1 to 10:
Java Code:int max = 10; Random random = new Random(); int myRandomInt = random.nextInt(max) + 1; //violá
- 12-18-2008, 02:03 AM #3
can you show me an example of what you mean please :(
>> What can be asserted without proof can be dismissed without proof. <<
- 12-18-2008, 02:03 AM #4
hhhmmm...
A couple of ideas:
orJava Code:if (n==0) { n=1; }
not sure on the syntax of the above, but you get the ideaJava Code:int randInt = rand.nextInt(n) [B]+ 1[/B];
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
look above
- 12-18-2008, 03:31 AM #6
What?
How in hell did you do that Furball? :) That was sneaky... Oh, well... the OP now has lots of options to pick from....
And speaking of the OP, Ciwan, what do you mean you need an example? Fubarable's post is a complete and functional example (more code than I posted).
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-18-2008, 06:21 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
By the way, always add 1. If you do the other suggestion (testing for 0 and replacing with 1), then the result will be biased towards producing 1 (since there are now two values that can end up with this number).
Neil Coffey
Javamex - Java tutorials and performance info
- 12-18-2008, 02:31 PM #8
>> What can be asserted without proof can be dismissed without proof. <<
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
trying to add up random numbers into one number
By pjr5043 in forum New To JavaReplies: 4Last Post: 09-15-2008, 02:20 PM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks