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

    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
    Northern Virginia, United States
    Rep Power

    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.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on 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, 06: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, 01:57 AM
  3. Powerball Example (Generating Random Numbers)
    By a7x75 in forum New To Java
    Replies: 8
    Last Post: 02-12-2010, 03:38 PM
  4. Replies: 12
    Last Post: 09-23-2009, 10:14 AM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 04: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