Random number between a range

hi all!
how can i find random number between an specified range
for example b/w 5-10
thanks to all
smh

Here's a practical instance;-import java.util.Random; // Maybe you forgot this
public class ImplementRandom {
  public void getRandom(int from, int range) {
     int newRandom;
     Random random = new Random(); 
     newRandom = from + random.nextInt(range);
     System.out.print(newRandom); 
  public static void main(String[] args) {
     ImplementRandom IC = new ImplementRandom();
     IC.getRandom(1, 100);
}Actually that uses public void rather than public int as you cannot reference a non-static (return-type) method from a static context.

Similar Messages

  • Random number between a range fix

    Hello
    I need to place an object on the stage.x randomly but not going .
    The stage is 1024 pixel and the object 726. I do the following, it works ok but sometimes some part of the object goes of the stage. Could you tell me what I do wrong?
    menucenter=Stage.width-_root.object._width;
    _root.object._x=Math.floor(Math.random() * (menucenter - 0)) - 0;
    Thank you
    George

    if the object has reg point at it's left edge use:
    _root.object._x=Math.random()*menucenter;

  • Random number within TWO ranges

    Is there anyway to select a random number within TWO ranges ? For example, a random number between the range (10 to 50) OR (100 to 200).
    Thank you !

    This is a simple class that produces a number between a range:
    class RandomRange
    static java.util.Random random= new java.util.Random();
    *    Return an int between n1 and n2
    static int getIntBetween(int n1, int n2) // n2 must be greater than n1 or n2-n1 must be positive
    // get random number between 0 and the range(n2 - n1)
    int result= random.nextInt(n2- n1);
    // plus the offset
    result += n1;
    return result;
    // return random.nextInt(n2 - n1) + n1;
    // the result is n2 exclusive, to make it inclusive return (result + 1)
    *    Return a double between d1 and d2
    static double getDoubleBetween(double d1, double d2)
    return random.nextDouble()*(d2 - d1) + d1;
    // d1 and d2 can be any value
    *    Return a float between f1 and f2
    static float getFloatBetween(float f1, float f2)
    // similar
    }----------------------------------

  • How to get a random number in a range?

    as title
    how to get a random number in a range?
    like 2000~3000 thks :)

    int between 10 and 20 with the method Math.random():
    public class Rnd
         // Test
         public static void main(String[] args)
             int start=10, end = 30;
             for (int i = 0; i < 10; i++)
                int n = (int)(start + Math.random() * (end-start));
                System.out.println(n);
    }best regards.

  • How I get random number between 1 and int number ?

    how can I get random number between 1 and int number
    like between 1 and 10 etc.
    10x

    Use the nextFloat() method of the Random class, this returns a random number between 0.0 and 1.0. To get and integer range from that multiply the return value of that method call by the integer range you need (in this case 10), and add the starting number you want returned in the range (1 in this case). The code below works for numbers 1-10.
    Random r = new Random();
    int x = (int)(r.nextFloat()*10) +1;

  • Random Number between 10 and 30

    I'm just curious how to generate a random number between 10 and 30. I know I use the Math.Random function, but I'm not certain how to do it when the starting number is >1 (i.e. 1 to 30). Thanks.

    Random Number in an Interval:
    ((int) (randomGeneratedNumber * difference)) + startIndex + 1
    * randomGeneratedNumber - Number >= 0 and <1
    * difference: To generate between 10 and 30, (30 -10 = 20)
    * startIndex: In this example, 10
    * +1: Neccesary adjustment.
    Example:
    ((int)(randomGeneratedNumber * 20)) + 10 + 1
    PD. Int cast it's to generate int numbers, so it's optional.

  • Get a random number between 2 numbers?

    Are their any classes which can produce a random number within a set of numbers i specify?
    If their are no predefined java classes then it doesnt matter.
    Thanks

    No as far as I know, but its easy to create one:
    double low_Limit, super_Limit;
    double interval = super_Limit - low_Limit;
    //Generate a random number between 0 and 1, with a Java satandard class
    double random_number = Math.random();
    //Now we generate a random numbre between 0 and intervalo:
    random_number = random_number * intervalo;
    // now we generate a random number between low_Limit super_Limit:
    random_number = random_number + low_Limit;
    //done :)
    Abraham.

  • How to Build a VI that generates a random number between 1 and 80 ....Help!!!!

    Build a VI that generates a random number between 1 and 80 
    Divides the random number by a number specified in the front panel.
    If the inputted number is zero, the VI program should turn on a LED in the front-panel to indicate a divide by zero error.
    Solved!
    Go to Solution.

    One quick question Edward, was the answer just in time for your monday morning class.
    Ton
    Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
    Nederlandse LabVIEW user groep www.lvug.nl
    My LabVIEW Ideas
    LabVIEW, programming like it should be!

  • Random number between 1 & 16?

    Hello all.  I need to create a variable that holds a random number  between 1 & 16.  Any idea how I can do this?  This is the way the  variable looks right now:
    var randomTurkey:Number = Math.floor(Math.random()*100);

    Never mind, I got it.  Thanks!

  • Random number between .2 and 1

    How do I generate a random number between 0.2 and 1.0

    ... or for - eg for 2 decimal places try this;-
    java.util.Random rand = new java.util.Random();
    int i = 20 + rand.nextInt(80);
    double d=(double)i;
    d /=100;

  • Re: Generating a random number between 1-10;

    How do I generate a number between 1-20? I saw how to on this forum once, but I can't find it, and I don't have much time to look. Sorry for any inconvinience. What I want to do is assign to the variable x the random number.
    Thanks.
    Virum

    Random rnd = new Random();
    int x = rnd.nextInt(20)+1;

  • SsRandom or another way to get a random number

    In iDocScript, I need to get a random number between 0 and 4, or it could be between 1 and 5. I found ssRandom, but am unsure how to use that number to get the range I want. Any other ways or advice?

    I found the answer, below is some iDocScript for generating random numbers.
    [!--$randomIndex = ssRandom()--]
    [!--$if randomIndex lt 0--]
    [!--$randomIndex = randomIndex * (-1)--]
    [!--$endif--]
    [!--$randomIndex = randomIndex % 5--];

  • Need Random "Number" Generation

    Hi...
    Need an easy way to generate a random "number" from an ABAP program... that is a combination of numbers and alphabetic characters.
    Is there a Function Module I did not find?
    Currently using Function module 'QF05_RANDOM_INTEGER' but this only generates a number.
    Thanks for your insights !
    Dave...

    Hi,
    I wrote a little method to generate a random number between 1 and 100 (can be modified to allow bigger range of numbers).  The code is below...  It basically gets a timestamp into a field of type timestampl (note the letter L at the end of the type).  The method signature consists of a single returning parameter called RANDOM_NUMBER (type I).  After much testing, the random sequence of numbers is extremely varied (even without a seed).
    Since 'QF05_RANDOM_INTEGER' already exists, then my method is not needed here, but I thought I'd include it in this thread since the topic was brought up.
    Cheers,
    James G.
    METHOD GENERATE_RANDOM_NUMBER .
      DATA:
        delay_cnt     TYPE i,
        lc_ran_seed   TYPE p,
        lc_time       TYPE timestampl,
        ran_num       TYPE p DECIMALS 7.
    * Get the long time stamp (nano second level)
      GET TIME STAMP FIELD lc_time.
    * Get the decimal part of the time stamp and shift it so that only the
    * last decimal values are used (i.e. the smallest unit of time)
      ran_num = frac( lc_time ).
      ran_num = ran_num * 10000.
    * Perform a delay with a varying length of time so that the next time
    * stamp will be more "random" if the method is called multiple times
    * (e.g. if called in a do loop to generate many random numbers)
      delay_cnt = ran_num * 2.
      DO delay_cnt TIMES. ENDDO.
    * Get the decimal part of the shifted time stamp, multiply it by 100 to
    * shift it to the left 2 decimal places, and add 1 to the result.
      ran_num = frac( ran_num ).
      lc_ran_seed = ( ran_num * 100 ) + 1.
      random_number = lc_ran_seed.
    * If the number generated is not between 1 and 100, then try again.
      IF random_number IS INITIAL OR random_number < 1
                                  OR random_number > 100.
        CALL METHOD me->generate_random_number
          RECEIVING
            random_number = random_number.
      ENDIF.
    ENDMETHOD.

  • How to generate random numbers in a range?

    Hello. i am having trouble finding out how to generate a random number in a certain range. For example i have an array of 100 elements and i want to access one of the elements between 50 and 60, however i cannot find how to generate a random number between these two values.
    Thanks in advance

    java.util.Random.nextInt(int)
    Note that you can get a number between 0 and (max -
    min), exclusive. Then add min, and you've got your
    number.Obviously you didn't read this. It says to get a number between 0 and (max - min) which is actually what you did. But after that you need to add min. The first step would get a number in between 0 and 5. When you add 20 you'd get a number between 0+20=20 and 5+20=25.

  • Generating N-digit random number??

    Hi All,
    I need to generate N-digit random number, anyone can help me?
    Thx in advance b4...

    import java.util.*;
    public class Try{
         public static void main(String []args){
              for(int i=0;i<100;i++){
                    System.out.println((int)(Math.random() * 10));
    }Yes I saw the casting, no problem with it, but I did used it alot to generate me number between a range of 1 - 9
    and if you want a range of 1 - 10, i just put a
    1+(int)(Math.random() * 10)

Maybe you are looking for