Results 1 to 2 of 2
  1. #1
    Zarquad is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Generating random numbers.

    How can I generate a random number between a range of numbers using a number generated by Math.random ()? I know in order to get a number between 0 and n, I simply have to multiply Math.random () by n, but I can't figure out how to do it between a range of m and n. Any ideas? It's an exercise in a textbook I'm learning from:

    Exercise 10.1 Write a method called randomDouble that takes two doubles, low
    and high, and that returns a random double x so that low  x < high.

    Exercise 10.2 Write a method called randomInt that takes two arguments, low and
    high, and that returns a random integer between low and high (including both).

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Generating random numbers.

    Try this on some paper. Write down a bunch of random numbers between 0 and n. Then add some constant m to each one. See what happens.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Generating random numbers
    By chinedum in forum New To Java
    Replies: 9
    Last Post: 09-21-2011, 05:03 AM
  2. I need help generating random numbers for a fill array
    By william979 in forum New To Java
    Replies: 15
    Last Post: 05-04-2011, 12:57 AM
  3. Powerball Example (Generating Random Numbers)
    By a7x75 in forum New To Java
    Replies: 8
    Last Post: 02-12-2010, 02:38 PM
  4. Replies: 12
    Last Post: 09-23-2009, 09:14 AM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •