Help Using a range of random numbers

so what i understand from this im learning flash from lynda.com tutorials and no matter how many times i listen to each word the guy says, i dont understand this part
in the vid, we are making a die to roll from 1-6 although the code below is not the finali hope someone can help me unserstand
var randomNumber:Number = Math.round(Math.random() * (10 - 1) + 1);
trace(randomNumber);
so what i understand from this is:
The variables name is randomNumber:randomNumber is going to be a Number = Using the Math class - we are going to round the Number(we make a random Number from 0 to 1 not including 1() * from here i need help understanding, please help me

Hi,
Math.random() will give you a number between 0 and 0.9999999 ( but not 1 );
lets suggest that you want a random number from 1 to 10. Note that round give you a digit from "0", so you must substract 1  ( 10 - 1) ( 0-10 fave 11 digits )
To get a random number of Some digit you will need to multiply Math.random() with this digit -1 - so if yo want to get a random digit from 10 you will need to get a random digit ( Math.random() ) and multiply it by 10-1. this will give you a randum number between 0 and 8.999(9).
but this have a floating part. To remove the "." you must round the result so after rounding the result will be a randm digit from  0 to 9. but the first digit is '0" if you dont neededthe result must be increment + 1 after rounding  and will be from 1 to 10

Similar Messages

  • Hi, I have an iPhone 4 which rings people when it is locked, whether or not in my pocket.  I always lock it when not using it.  Can anyone help? It is calling random numbers, not the last person or anyone even recently called on my recents list. Thanks

    Hi, I have an iPhone 4 which rings people when it is locked, whether or not in my pocket.  I always lock it when not using it.  Can anyone help? It is calling random numbers, not the last person or anyone even recently called on my recents list. Thanks

    Pay no attention to iinami, the amount of replies to people saying their handsets must have been jailbroken everytime iTunes throws out an error is tremendous. (Clearly you don't need to have any real knowledge to get to level 3 on these forums, let's hope apple's geniuses know a lot more than some of their customers.)
    http://support.apple.com/kb/TS3694
    Solution below.
    Error 9
    This error occurs when the device unexpectedly loses its USB connection with iTunes. This can occur if the device is manually disconnected during the restore process. This issue can be resolved by performing USB troubleshooting, using a different USB dock-connector cable, trying another USB port, restoring on another computer, or by eliminating conflicts from third-party security software.

  • Help in generating the same random numbers each time of the program executi

    dear friends
    i am facing a problem that the random numbers generated at time of each exectuion of the program are not the same, i want to generate the same random numbers every time time i run the program. i need your help. i am giving the code in c++ if anybody can help in providing the solution so that i get the same random numbers at every run of the program. waiting for your help
    wit regards
    jaya shankar
    #include<iostream.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    class density
    int s[150][150],parent[150][150],i,j,l,n,loop;
    public:
    void bd(void);
    void css(void);
    void density :: bd(void)
    cout<<"ENTER THE POPULATION SIZE = ";
    cin>>n;
    cout<<"\nENTER THE NO.OF LOOPS = ";
    cin>>loop;
    for(i=0;i<n;i++)
    for(j=0;j<80;j++)
    s[i][j]=rand()%2;
    cout<<"s:"<<s[i][j];
    void density :: css(void)
    int a,b;
    float c;
    for(i=0;i<n;i=i+2)
         b=rand()%100;
         cout<<"random b="<<b;
    main()
    density d;
    d.bd();
    d.css();

    To generate the same random,it's impossible!
    why do you want to do that?

  • Help in generating the same random numbers at every time of executuion

    dear friends
    i am facing a problem that the random numbers generated at time of each exectuion of the program are not the same, i want to generate the same random numbers every time time i run the program. i need your help. i am giving the code in c++ if anybody can help in providing the solution so that i get the same random numbers at every run of the program. waiting for your help
    wit regards
    jaya shankar
    #include<iostream.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    class density
    int s[150][150],parent[150][150],i,j,l,n,loop;
    public:
    void bd(void);
    void css(void);
    void density :: bd(void)
    cout<<"ENTER THE POPULATION SIZE = ";
    cin>>n;
    cout<<"\nENTER THE NO.OF LOOPS = ";
    cin>>loop;
    for(i=0;i<n;i++)
    for(j=0;j<80;j++)
    s[j]=rand()%2;
    cout<<"s:"<<s[j];
    void density :: css(void)
    int a,b;
    float c;
    for(i=0;i<n;i=i+2)
    b=rand()%100;
    cout<<"random b="<<b;
    main()
    density d;
    d.bd();
    d.css();

    You have to read the documentation for the java.util.Random class.
    "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random"
    Here is a peace of code that will do wat you need:
    Random rand = new Random(100);
    for(int i=0; i<10; i++)
         System.out.println("Random="+ rand.nextInt(10));

  • Using a range to input numbers to a table

    Hi i have written this bit of code to allow numbers to be written to the table. I use "b" as the limit the numbers go to but when i want to use "a" as the start point it returns the error loss of precision. E.g. i enter a = 3 and b = 8 and in the table it returns 3, 4, 5, 6, 7, 8 in a single column. The error message reads "Loss of possible precision" and
    found : double
    required: int
    for (int row = a; row <= b; row++)
    1 error
    BUILD FAILED (total time: 0 seconds)
    double a = Double.parseDouble(txtRangeFrom.getText());
        double b = Double.parseDouble(txtRangeTo.getText());     
        int i = 1;
        for (int row = a; row <= b; row++)
            for (int col = 1; col <= 1; col++){
            tblOut.setValueAt(Double.toString(i),row,col);
            i++;
        }Thanks any help would be appreciated

    Hi i have written this bit of code to allow numbers
    to be written to the table. I use "b" as the limit
    the numbers go to but when i want to use "a" as the
    start point it returns the error loss of precision.
    E.g. i enter a = 3 and b = 8 and in the table it
    returns 3, 4, 5, 6, 7, 8 in a single column. The
    error message reads "Loss of possible precision" and
    found : double
    required: int
    for (int row = a; row <= b; row++)
    ror
    BUILD FAILED (total time: 0 seconds)
    double a =
    Double.parseDouble(txtRangeFrom.getText());
    double b =
    Double.parseDouble(txtRangeTo.getText());     
    int i = 1;
    for (int row = a; row <= b; row++)
    for (int col = 1; col <= 1; col++){
    tblOut.setValueAt(Double.toString(i),row,col);
    i++;
    ode]
    Thanks any help would be appreciatedrow is an int and you're trying to compare it to b which is a Double.....

  • Create Random Numbers using OLE Randomize and RND

    Hi,
    I've managed to create an excel spreadsheet by creating an object of type EXCEL.APPLICATION. I've also managed to rename the worksheet tabs, move from one tab to another as well as fill certain cells with numbers.
    I would now like to create a range of random numbers within one of these worksheets. I believe I should be using the Randomize and RND methods contained within the VBA OLE object. This is what I've done so far :
    DATA: gs_vba TYPE ole2_object,           "VBA object
               gs_math TYPE ole2_object.
    CREATE OBJECT gs_vba 'VBA'.
    CALL METHOD OF gs_vba 'MATH' = gs_math.
    CALL METHOD OF gs_math 'Randomize' = gs_math.
    CALL METHOD OF gs_math 'Rnd' = gs_math
      EXPORTING
        #1 = 1000.
    Is the above the right way to do it? If not, I would really appreciate your help, and if possible, some sample code to do this as well.
    Thanks and regards,
    Adeline.

    Hi,
    I've managed to create an excel spreadsheet by creating an object of type EXCEL.APPLICATION. I've also managed to rename the worksheet tabs, move from one tab to another as well as fill certain cells with numbers.
    I would now like to create a range of random numbers within one of these worksheets. I believe I should be using the Randomize and RND methods contained within the VBA OLE object. This is what I've done so far :
    DATA: gs_vba TYPE ole2_object,           "VBA object
               gs_math TYPE ole2_object.
    CREATE OBJECT gs_vba 'VBA'.
    CALL METHOD OF gs_vba 'MATH' = gs_math.
    CALL METHOD OF gs_math 'Randomize' = gs_math.
    CALL METHOD OF gs_math 'Rnd' = gs_math
      EXPORTING
        #1 = 1000.
    Is the above the right way to do it? If not, I would really appreciate your help, and if possible, some sample code to do this as well.
    Thanks and regards,
    Adeline.

  • HELPwith array of random numbers!!!

    can anyone please write up a program that uses an array of random numbers from 0-100 and sort them out...
    if anyone can help me that would be great...i tried but i cannot figure it out...
    please just write the program so I just have to copy and paste :)

    This has to be the most asked question here, or at least it's way up there with classpath and path questions.
    Use the search feature to search the forum for random number generation and I swear you'll find more answers and code than you could possibly want.
    Good luck
    Lee

  • Figuring out the occurence of random numbers?

    So, I'm taking this Computer Science class, and I've been struggling so much with today's lesson.
    For this particular question, I have to get 20 random numbers between 1-5, represent them in text format, and the show how often each of the numbers occured. I've done the first two of the three parts, but this last part really has me stumped..
    (Note: I'm using Ready to Program by Holt.... so I'm using their console)
    Here's the code I got so far:
    import java.util.*;
    import hsa.Console;
    import java.io.*;
    public class RandomNumbers
        static Console c;
        static public void main (String[] args) throws IOException
            c = new Console ();
            int a;
            Random generator = new Random();
            c.println("Generating Numbers: ");
            for(a=1;a<=20;a++)
                int r = generator.nextInt(5)+1;
                    switch(r)
                         case 1: c.print("One",10); break;
                         case 2: c.print("Two",10); break;
                         case 3: c.print("Three",10); break;
                         case 4: c.print("Four",10); break;
                         case 5: c.print("Five",10); break;
    }}

    I had a similar assignment my last semester for C++. I would think the general idea is the same.
    Here I declare an Array. Then I store the values of the dice rolled in the array and display it on the screen. The syntax will be different since you are not using C++.
    #include <iostream>          // preprocessor directive
    #include <ctime>          // preprocessor directive
    #include <cmath>          // preprocessor directive
    #include <cstdlib>          // preprocessor directive
    using namespace std;     // enables preprocessor directives
    int random_int(int a, int b);          // function prototpye
    int main ()     // main function
         srand(time(NULL));          // enables random numbers to be generated
         int d1, d2, sum;                         // declaring variables
         int a[10] = {0,0,0,0,0,0,0,0,0,0};     // initializes valus in array to 0
         int roll;                                   // declaring variable
         cout << "Roll how many times?";          // ask number of times to roll dice
         cin >> roll;                              // get number of times to roll dice
         for(int i=0; i<=roll; i++)               // to roll the dice the numbers or times asked
              d1 = random_int(1,6);               // generates number for die 1
              d2 = random_int(1,6);               // generates number for die 2
              sum = d1 + d2;                         // adds the two dice together     
              if(sum == 2)                         // increase value in array  by 1 that corresponds to 2
                   a[0]++;
              else if(sum==3)                         // increase value in array  by 1 that corresponds to 3
                   a[1]++;
              else if(sum==4)                         // increase value in array  by 1 that corresponds to 4
                   a[2]++;
              else if(sum==5)                         // increase value in array  by 1 that corresponds to 5
                   a[3]++;     
              else if(sum==6)                         // increase value in array  by 1 that corresponds to 6
                   a[4]++;
              else if(sum==7)                         // increase value in array  by 1 that corresponds to 7
                   a[5]++;
              else if(sum==8)                         // increase value in array  by 1 that corresponds to 8
                   a[6]++;
              else if(sum==9)                         // increase value in array  by 1 that corresponds to 9
                   a[7]++;
              else if(sum==10)                    // increase value in array  by 1 that corresponds to 10
                   a[8]++;          
              else if(sum==11)                    // increase value in array  by 1 that corresponds to 11
                   a[9]++;
              else if(sum==12)                    // increase value in array  by 1 that corresponds to 12
                   a[10]++;
         for(int j = 2; j<=12; j++)                    
              cout << j << "  " << a[j-2] <<endl;          // output the array
         return 0; //exit program
    int random_int(int a, int b)          // used to generate te random numbers
         int random = rand()%(b-a+1) + a;
         return (random);
    }          

  • How to generate random numbers in a range using random class?

    I know how to use Math.random for this, but how would I generate random numbers using the random class?
    Say I want a number between 40 and 50, inclusive--how would I do this?
    What i have in mind is:
    int randomNumber = random.nextInt(max) + min;
    where max is 50 and min is 40. Is this correct?

    Fredddir_Java wrote:
    I know how to use Math.random for this, but how would I generate random numbers using the random class?
    Say I want a number between 40 and 50, inclusive--how would I do this?
    What i have in mind is:
    int randomNumber = random.nextInt(max) + min;
    where max is 50 and min is 40. Is this correct?What happened when you generated a couple hundred numbers that way? Did you get all the ints in the range you wanted?

  • I need help with this program ( Calculating Pi using random numbers)

    hi
    please understand that I am not trying to ask anymore to do this hw for me. I am new to java and working on the assignment. below is the specification of this program:
    Calculate PI using Random Numbers
    In geometry the ratio of the circumference of a circle to its diameter is known as �. The value of � can be estimated from an infinite series of the form:
    � / 4 = 1 - (1/3) + (1/5) - (1/7) + (1/9) - (1/11) + ...
    There is another novel approach to calculate �. Imagine that you have a dart board that is 2 units square. It inscribes a circle of unit radius. The center of the circle coincides with the center of the square. Now imagine that you throw darts at that dart board randomly. Then the ratio of the number of darts that fall within the circle to the total number of darts thrown is the same as the ratio of the area of the circle to the area of the square dart board. The area of a circle with unit radius is just � square unit. The area of the dart board is 4 square units. The ratio of the area of the circle to the area of the square is � / 4.
    To simuluate the throwing of darts we will use a random number generator. The Math class has a random() method that can be used. This method returns random numbers between 0.0 (inclusive) to 1.0 (exclusive). There is an even better random number generator that is provided the Random class. We will first create a Random object called randomGen. This random number generator needs a seed to get started. We will read the time from the System clock and use that as our seed.
    Random randomGen = new Random ( System.currentTimeMillis() );
    Imagine that the square dart board has a coordinate system attached to it. The upper right corner has coordinates ( 1.0, 1.0) and the lower left corner has coordinates ( -1.0, -1.0 ). It has sides that are 2 units long and its center (as well as the center of the inscribed circle) is at the origin.
    A random point inside the dart board can be specified by its x and y coordinates. These values are generated using the random number generator. There is a method nextDouble() that will return a double between 0.0 (inclusive) and 1.0 (exclusive). But we need random numbers between -1.0 and +1.0. The way we achieve that is:
    double xPos = (randomGen.nextDouble()) * 2 - 1.0;
    double yPos = (randomGen.nextDouble()) * 2 - 1.0;
    To determine if a point is inside the circle its distance from the center of the circle must be less than the radius of the circle. The distance of a point with coordinates ( xPos, yPos ) from the center is Math.sqrt ( xPos * xPos + yPos * yPos ). The radius of the circle is 1 unit.
    The class that you will be writing will be called CalculatePI. It will have the following structure:
    import java.util.*;
    public class CalculatePI
    public static boolean isInside ( double xPos, double yPos )
    public static double computePI ( int numThrows )
    public static void main ( String[] args )
    In your method main() you want to experiment and see if the accuracy of PI increases with the number of throws on the dartboard. You will compare your result with the value given by Math.PI. The quantity Difference in the output is your calculated value of PI minus Math.PI. Use the following number of throws to run your experiment - 100, 1000, 10,000, and 100,000. You will call the method computePI() with these numbers as input parameters. Your output will be of the following form:
    Computation of PI using Random Numbers
    Number of throws = 100, Computed PI = ..., Difference = ...
    Number of throws = 1000, Computed PI = ..., Difference = ...
    Number of throws = 10000, Computed PI = ..., Difference = ...
    Number of throws = 100000, Computed PI = ..., Difference = ...
    * Difference = Computed PI - Math.PI
    In the method computePI() you will simulate the throw of a dart by generating random numbers for the x and y coordinates. You will call the method isInside() to determine if the point is inside the circle or not. This you will do as many times as specified by the number of throws. You will keep a count of the number of times a dart landed inside the circle. That figure divided by the total number of throws is the ratio � / 4. The method computePI() will return the computed value of PI.
    and below is what i have so far:
    import java.util.*;
    public class CalculatePI
      public static boolean isInside ( double xPos, double yPos )
         double distance = Math.sqrt( xPos * xPos + yPos * yPos );        
      public static double computePI ( int numThrows )
        Random randomGen = new Random ( System.currentTimeMillis() );
        double xPos = (randomGen.nextDouble()) * 2 - 1.0;
        double yPos = (randomGen.nextDouble()) * 2 - 1.0;
        int hits = 0;
        int darts = 0;
        int i = 0;
        int areaSquare = 4 ;
        while (i <= numThrows)
            if (distance< 1)
                hits = hits + 1;
            if (distance <= areaSquare)
                darts = darts + 1;
            double PI = 4 * ( hits / darts );       
            i = i+1;
      public static void main ( String[] args )
        Scanner sc = new Scanner (System.in);
        System.out.print ("Enter number of throws:");
        int numThrows = sc.nextInt();
        double Difference = PI - Math.PI;
        System.out.println ("Number of throws = " + numThrows + ", Computed PI = " + PI + ", Difference = " + difference );       
    }when I tried to compile it says "cannot find variable 'distance' " in the while loop. but i thought i already declare that variable in the above method. Please give me some ideas to solve this problem and please check my program to see if there is any other mistakes.
    Thanks a lot.

    You've declared a local variable, distance, in the method isInside(). The scope of this variable is limited to the method in which it is declared. There is no declaration for distance in computePI() and that is why the compiler gives you an error.
    I won't check your entire program but I did notice that isInside() is declared to be a boolean method but doesn't return anything, let alone a boolean value. In fact, it doesn't even compute a boolean value.

  • How do I generate an array of random numbers that relate to an output wave that falls within a certain frequency range?

    I have been creating random numbers that I'm using within a system, the system is working fine, but now I have realised that the random numbers must be outputted to speakers in such a way as to filter out all but a low frequency range.
    I was thinking about generating a dither signal with a bandpass filter, but could not get it to give me out a full array of 250 values which I could then manipulate.
    The sampling frequency of the output is 200Hz and the 250 data points must fall within the 0-100Hz range.
    Another course of action I am considering is to use an FFT and an inverse FFT to get the data that I'm looking for, but I'm fair
    ly inexperienced with using labVIEW and can't quite get it to work.
    Thanks for the help,
    Hank.

    As you may already know, in the Functions palette/Analysis VIs/Filters VIs are VIs for filtering. There are also some examples for using these filters under Examples/Analysis/fltrxmpl. You may find some of these are related to your situation, especially for filtering out the higher values.
    An option is to simply output the random values to a Comparison/"less than or equal" function, where only values <= to 100 would be sent to an array which is in a For loop. Use the iteration counter of the For loop to count up to 250, at which time you would pass the whole array to your next node. This would let you have 250 values between 0 and 100 for your final array.
    Good luck.

  • Array of unique random numbers - help?

    basically, i'm trying to create a BINGO card.
    i stared learning Java a week ago and am finding it easy to learn and use....at least so far.
    my instructor gave me the assignment to created a BINGO card. so i used the system.out.format command to make 5 tab fields, 8 spaces each ("%8s %8s %8s %8s %8s", "B", "I", "N", "G", "O"). although the assignment only wanted me to think numbers out of thin air, just to use as examples, i went above and beyond with the idea to make a BINGO card generator that could actually function.
    then i started the random number sequences, using the Math.random() command. all told, there are 24 of these in the program, one for each number on a bingo card. the field in the middle is the FREE space, so it has no Math.random command.
    in BINGO, each letter (one of my five tab fields) can have a number in ranges of 15. B is 1 to 15, I is 16 to 30, etc. it looks similar to this:
    B I N G O
    9 19 39 57 66
    3 28 32 51 74
    3 29 FREE 46 70
    14 28 43 55 67
    9 24 35 59 62
    as you can tell, i'm having trouble with actually making unique random numbers so that none repeat.
    is there a command or string or something to help me accomplish this?

    The best way I've come up with is to use an object to store the numbers that implements Collection--like ArrayList...
    Then you load it with the range of numbers that you want and call, shuffle() on the object, that will randomize your range of numbers and then you can choose the quantity you want from the storage object. So let's say you need 25 number in the range of 1 to 100:
    Add the numbers (you have to use classes so I would do Integers) 1 to 100;
    call shuffle()
    pull back the first 25 numbers
    This will guarantee that each number is distinct and in a random order.
    If you need multiple sheets populated, you can just call shuffle() between population of each sheet.
    package Junk;
    import java.util.ArrayList;
    import java.util.Collections;
    class Junk{
      private ArrayList<Integer> l;
      Junk(){
        l = new ArrayList<Integer>();
      public void loadList(int s, int e){
        for(int i=s; i<=e; i++){
          l.add(new Integer(i));
      public void randomizeList(){
        Collections.shuffle(l);
      public void printList5(){
        for(int i=0; i<5; i++){
          System.out.println(l.get(i));
      public static void main(String[] args){
        Junk j = new Junk();
        j.loadList(10,99);
        j.randomizeList();
        j.printList5();
        System.out.println();
        j.randomizeList();
        j.printList5();
    }

  • Please Help in generation of Random Numbers & Characters

    hai friends,
    Please help me to generate random Numbers and Characters to the Fields of the Table.
    Please Send code to it.
    madhava

    It's easier enough. Just use the DBMS_RANDOM package supplied by Oracle.
    SQL> desc rndm
    Name                                      Null?    Type
    COL1                                               NUMBER
    COL2                                               VARCHAR2(10)
    COL3                                               DATE
    SQL> BEGIN
      2     DBMS_RANDOM.initialize(12348765);
      3     FOR i IN 1..4 LOOP
      4        INSERT INTO rndm VALUES (dbms_random.value
      5                                 , dbms_random.string('A', 10)
      6                                 , sysdate + dbms_random.value(1,1000));
      7     END LOOP;
      8  END;
      9  /
    PL/SQL procedure successfully completed.
    SQL> SELECT * FROM rndm;
          COL1 COL2       COL3
    .028766128 pUFMxywBzC 14-JAN-07
      .7524715 SupFLVZmEC 22-JUN-07
    .454116997 EHCEZKPORm 14-FEB-05
    .330733477 gycpybxdRS 12-MAR-07
    SQL> Note that the published API seems to have changed in 9.2 (if not earlier), with the apparent sole purpose of DBMS_RANDOM being the generation of random numbers. However, as you can see, the old methods still work. Not sure what Oracle are up to.
    Cheers, APC

  • I have just upgraded to the new OS X V. 10.10.3 but cannot access my iCloud Drive documents using the resident Pages and Numbers software on my MacBook Pro. Help needed.

    I have just upgraded to the new OS X V. 10.10.3 but cannot access my iCloud Drive documents using the resident Pages and Numbers software on my MacBook Pro. Help is needed to access those documents using the resident software on my MacBook Pro rather than the Beta software on iCloud.com.

    I have iCloud Drive set on the Finder sidebar and use that to open the Numbers Spreadsheet on iCloud.
    OSX 10.10.3
    Best.

  • How can I perform the LINEST function using the new version of Numbers? The old method of using "command enter" to complete the array function does not work. Please help ASAP!!!

    LINEST function using Numbers for Mac

    Thank you for your help. This page is somewhat helpful, but I still don't quite understand.  I am using the Numbers program, and my assignment calls for me to use the LINEST function to return a set of 10 values. 
    When I googled how to do this, a lot of pages mentioned hitting "command+enter" to complete the array function, but they were using an older version of Numbers and it doesn't do anything when I try it in the new version.  I tried using "Index" with LINEST, and i've managed to get about 5 values out of it.

Maybe you are looking for