Generating Random Numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tiktik
    New Member
    • Nov 2008
    • 14

    Generating Random Numbers

    Hey,
    I had school exercise in which i needed to generate some numbers within the range of 70 to 90. I'd like to know whether there exists an exact formula which allows the numbers to be generated within a particular range.


    When I had to generate numbers from 10 to 20, the correct code was :

    float a = (float) (Math.random()* 10) +10;

    but I can't establish a general rule for all the ranges...

    Any ideas pls??
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    OK, let's have a look at what that formula does.
    Code:
    float a = (float) (Math.random()*10) +10; // Your formula
    
    double x = Math.random; // create a pseudo-random number with 0.0 <= x < 1.0
    x = x * 10;             // multiply that random number with 10, so you'll have a random number 0.0 <= x < 10.0
    float a = (float) x;    // cast that double (64 bit) to float (32 bit)
    a = a + 10;             // add 10 to a, so you'll have a variable 10.0 <= a < 20.0
    With that explanation, can you write code to create a random number between 70 and 90?

    Greetings,
    Nepomuk

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      If you have to generate numbers in the range [lo,hi) (inclusive and exclusive), you
      have to generate numbers in the range lo+[0, hi-lo), so if R is a random number
      in the range [0, hi-lo) then lo+R is the wanted random number. If you can only
      generate random numbers in the range [0,1) == R' then R == (hi-lo)*R'

      kind regards,

      Jos

      Comment

      • tiktik
        New Member
        • Nov 2008
        • 14

        #4
        Originally posted by Nepomuk
        OK, let's have a look at what that formula does.
        Code:
        float a = (float) (Math.random()*10) +10; // Your formula
        
        double x = Math.random; // create a pseudo-random number with 0.0 <= x < 1.0
        x = x * 10;             // multiply that random number with 10, so you'll have a random number 0.0 <= x < 10.0
        float a = (float) x;    // cast that double (64 bit) to float (32 bit)
        a = a + 10;             // add 10 to a, so you'll have a variable 10.0 <= a < 20.0
        With that explanation, can you write code to create a random number between 70 and 90?

        Greetings,
        Nepomuk
        Thanks a lot ... i think i've found it :

        int a = (int) (Math.random()* 20) +70;

        isn't it?...
        (it does work)

        thanks again =)

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by tiktik
          int a = (int) (Math.random()* 20) +70;
          Yes, well done! :-) (Although you're using an integer here and a float in your example - any specific reason for that?)

          Greetings,
          Nepomuk

          Comment

          Working...