• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Random generator - urgent

Hi,

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.

Math.random()

Pls help.thanks
0
Jasbir21
Asked:
Jasbir21
3 Solutions
 
CEHJCommented:
java.util.Random should be OK
0
 
Jim CakalicSenior Developer/ArchitectCommented:
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: http://dsd.lbl.gov/~hoschek/colt/

Regards,
Jim Cakalic
0
 
borislavmarkovCommented:
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)));

    }
}
0
 
Jasbir21Author Commented:
Thanks a lot
0
 
CEHJCommented:
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now