OrangeSoul Posted July 7, 2004 Share Posted July 7, 2004 I need some help with creating random numbers like for instance a simulation for baseball game results . if you know what I mean Link to comment Share on other sites More sharing options...
0 Cody Posted July 7, 2004 Share Posted July 7, 2004 if you have the java docs you can look up the Random class java.util.Random i think Link to comment Share on other sites More sharing options...
0 OPaul Posted July 7, 2004 Share Posted July 7, 2004 http://java.sun.com/j2se/1.4.2/docs/api/ja...til/Random.html Link to comment Share on other sites More sharing options...
0 Winston Posted July 7, 2004 Share Posted July 7, 2004 (edited) The Random class is quite easy to use, as posted above the java documentation helps alot. Basically you just need to instantiate a new instance of the Random class and use the functions. This is an example of generating a Integer between the values of '1' and whatever parameter passed: public int generateRandomInt(int valueRange) { ? ?//Assuming java.util package is imported ? ?int generatedValue; ? ?Random randomiser = new Random(); ? ?generatedValue = 1 + randomiser.nextInt(valueRange); ? ?return generatedValue; } In this example i put the code where it instantiates a new instance of the Random class within the method, however it would be better to leave it outside. Edited July 7, 2004 by Winston Link to comment Share on other sites More sharing options...
0 Cody Posted July 7, 2004 Share Posted July 7, 2004 This is an example of generating a Integer between the values of 0 and 6: public int generateRandomInt(int valueRange) { ? ?//Assuming java.util package is imported ? ?int generatedValue; ? ?Random randomiser = new Random(); ? ?generatedValue = randomiser.nextInt(valueRange) + 1; ? ?return generatedValue; } actually i think this would return a int from 1-valueRange Link to comment Share on other sites More sharing options...
0 Winston Posted July 7, 2004 Share Posted July 7, 2004 actually i think this would return a int from 1-valueRange hmmm would adding 1 in front first make a difference, meh i haven't programmed in a few months, but adding a 1 is only so that it will include the end range, for example if the value range was 7 then it will generate from 0 (Inclusive) to 7 (Inclusive). Link to comment Share on other sites More sharing options...
0 Cody Posted July 7, 2004 Share Posted July 7, 2004 if you passed it 7, there are 7 numbers it coult return. and 0-7 are 8 numbers, so if you passed it 7 then added that 1 it owuld be 1-7, with out the +1 it woudl be 0-6 Link to comment Share on other sites More sharing options...
0 Winston Posted July 7, 2004 Share Posted July 7, 2004 if you passed it 7, there are 7 numbers it coult return. and 0-7 are 8 numbers, so if you passed it 7 then added that 1 it owuld be 1-7, with out the +1 it woudl be 0-6 Yes... but the nextInt method returns a range between 0 Inclusive, and to the specified range EXCLUSIVE, thus the need for the adding of 1. Ahh EDIT... i can see where you're coming from... yeah sorry my bad heh. Link to comment Share on other sites More sharing options...
0 OrangeSoul Posted July 8, 2004 Author Share Posted July 8, 2004 ok when using the random numbers how do i limit them to using numbers for instance between 60 and 110 for instance Link to comment Share on other sites More sharing options...
0 OrangeSoul Posted July 8, 2004 Author Share Posted July 8, 2004 Can anyone tell me how I can use Java to determine that the int number for team1 is greater than team2 or viceversa? Link to comment Share on other sites More sharing options...
0 mrvc Posted July 8, 2004 Share Posted July 8, 2004 I just use the random method from Math class Link to comment Share on other sites More sharing options...
0 mr.dan Posted July 8, 2004 Share Posted July 8, 2004 The easiest way is to use the random method from the math class with an integer parameter. Using bollean statmenets is the easiest way to check if number a is greater than b. Link to comment Share on other sites More sharing options...
Question
OrangeSoul
I need some help with creating random numbers like for instance a simulation for baseball game results . if you know what I mean
Link to comment
Share on other sites
11 answers to this question
Recommended Posts