I was wondering how can I generate a random number?

Top. Does Java include a random number function.

Comments

  • Hi.

    Take a look at the API:
    http://java.sun.com/j2se/1.3/docs/api/java/lang/Math.html#random()

    Cheers,
    Kyon

    -- planning is the substitution of hazard by error --

  • Yes, it has. You have to import java.util.Random; Then you make an instance of [italic]Random[/italic] and use it's function, for example [italic]nextInt[/italic]. There are also function for boolean, ...

    [code]
    import java.util.Random;
    ...
    Random random = new Random();

    // create random number between 0 and 4
    int i = random.nextInt(5);
    [/code]

    : Top. Does Java include a random number function.
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories