Random generator - urgent

Posted on 2006-04-10
Last Modified: 2010-03-31

In matlab, there is this random generator  that is called rand "that returns a pseudorandom, scalar value drawn from a uniform distribution on the unit interval."

Lets say I need to find the equilavent in java, is nextFloat, nextDouble or the Random generator itself.


Pls help.thanks
Question by:Jasbir21
    LVL 86

    Accepted Solution

    java.util.Random should be OK
    LVL 19

    Assisted Solution

    by:Jim Cakalic
    Math.random constructs an instance of Random to use for servicing getDouble calls. So Math.random and Random.getDouble are equivalent. If this is insufficient for your needs, you might have a look at the random package of Colt:

    Jim Cakalic
    LVL 2

    Assisted Solution

    import java.util.Random;

    public class Dummy {
        public static void main(String[] args)
            double rangeMin = 5;
            double rangeMax = 10;
            Random r = new Random(System.currentTimeMillis());
            System.out.println("INT:"    + ((int)rangeMin + r.nextInt((int)rangeMax-(int)rangeMin)));
            System.out.println("FLOAT:"  + (rangeMin + r.nextFloat()*(rangeMax-rangeMin)));
            System.out.println("DOUBLE:" + (rangeMin + r.nextDouble()*(rangeMax-rangeMin)));


    Author Comment

    Thanks a lot
    LVL 86

    Expert Comment


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now