• 0

Help Sites for Java Random Numbers etc


Question

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

  • 0

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 by Winston
Link to comment
Share on other sites

  • 0
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

  • 0
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

  • 0

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

  • 0
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

  • 0

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

  • 0

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

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.