Random numbers in midp

Am I missing something or is there no library method to generate random numbers in midp? if there isn't, can someone provide me with some link to a simple class that generates them or give some source.. I tried to use the date (time) to generate randoms but i need something more 'random'. And I'm sure there are many more tested and well working implementations etc. than I can come up with... :)

import java.util.Random;
import java.lang.Math;
public class MyRandom
{ //Start of Class
     private Random random;
     //Constructor
     public MyRandom()
          random = new Random();     
     //Will return a number between 0 and maxint
     public int getInt(int maxint)
          return Math.abs(random.nextInt()>>>1) % (maxint + 1);
     //Will return a number between teh startint and the maxint
     public int getInt(int startint, int maxint)
          return startint + (Math.abs(random.nextInt()>>>1) % ((maxint-startint) + 1));     
} //End of Class
Hope this helps

Similar Messages

  • I am trying to generate an array of 30 random numbers. after every 5 readings a new vi should open and tell the user that 5 readings has been completed. and again carry on with the generation of array.

    since i do not have a transducer now, i am currently generating an array of 30 random numbers. after every 5 readings a warning should be given to the user that 5 readngs are complete. this cycle should repeat. the array size is 30.
    please help me out,  waiting for reply asap.
    once i have the transducer, i will be taking 30 analog samples and then after every 5 smaples that wraning will be displaye din a new VI
    Solved!
    Go to Solution.

    Use a while loop with a time delay representing your sampling period.
    Use the count terminal to check if equals 4, so 4th iteration=5th sample.
    Use a case structure. The true case will only be executed on the 4th iteration.
    In the true case place a subVI  with your desired message in the front panel. Go to the VI properties window and set "open front panel when called".
    The closing condition of the warnign is not giving in your description.
    Consider that rather than usign a subvi for this, you could use the "One/Two/Three button dialog" or "display message" vis at the "dialog and user interface" pallete.
    Please give it a try and send your own VI. Do not expect us to provide a working solution.
    Regards,

  • How can you generate Multiple random numbers from 1 to 49?

    I am very new at programming with the iPhone SDK and I need some help with a project I am working on. What I want to do is set up a window with 6 labels and 1 button. When the user clicks the button, the program will populate 6 randomized numbers each ranging from 1 to 49, and then place each of 6 numbers in a label. But each time the program ends and starts again the numbers cannot be the same, and also when the user clicks the button, no label can have the same number twice, so for example. If label 1 had the number 10, the other 5 labels cannot have that number until the button is clicked again. I know how to set up the interface, I just need the code. I would so greatly appreciate someone's help in this matter. I have been trying to do this for days and I cannot figure it out. Possibly someone who knows tons about Objective C programming can help me!
    Thank-you so very much!!

    I see that you're writing a lottery number generator. Perhaps the easiest way to do it is to emulate a real lottery: fill an array (NSMutableArray, probably) with the numbers between 1 and 49, pick one at random, remove that number from the array, and repeat. (You can think of the numbers as being the balls and the array as being the machine that pops them out.)
    One simple way to get the random indices needed is to extract six random bytes from Randomization Services using SecRandomCopyBytes, then loop over them, using the modulo operator (%) to select an index within the size of the array.
    And no, I'm not going to write your code for you. If I was going to do that, I could package and sell the app myself.

  • 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.

  • Problem with Purchase Order creation with Random numbers .

    Hi Experts
    Currently i am facing an issue with Bapi BAPI_PO_CREATE1 to create Purchase order with random numbers for example items 1, 3,5.
    Please let me know the settings .
    Thanks in Advance

    Hi Neha,
    A reset of the release strategy only takes place if
    -  the changeability of the release indicator is set to '4' in
       case of a purchase requisition and '4' or '6' in case of
       another purchasing document (purchase order, request for
       quotation, contract, scheduling agreement),
    -  the document is still subject to the previous release
       strategy,
    -  the new TOTAL NET ORDER VALUE is higher than the old one.
    The total net order value is linked to CEKKO-GNETW for a purchase order,
    CEBAN-GSWRT for a purchase requisition item-wise release and CEBAN-GFWRT
    for a purchase requisition overall release.
    If you have maintained a Tolerance for value changes during release
    (V_161S-TLFAE), the release strategy is reset when the value of the
    document is higher than the percentage you have specified and if the
    document is still subject to the previous release strategy.
    Review the SAP note 365604 and 493900 for more
    information about Release strategy.
    Regards,
    Ashwini.

  • Program to create random numbers in plsql

    How do we Write a program to create random numbers.
    Thanks

    No need to - we have DBMS_RANDOM :)

  • 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

  • Can't validate outlook on iPad - random numbers and letters don't display - only a question mark in a blue box

    If I have to validate Outlook on PC or Mac - I have no problem - the random numbers and letters display fine - but if I am requested to validate outlook before sending a mail when using my iPad, the operation fails as the test numbers and letters are not shown - only a snap blue box with a question mark.  How do I convert the box into the image of a new validation code?  Thanks in advance.

    Hello,
    The question mark means it can't find it where it was when you put it in the Dock.
    I'd get EasyFind...
    http://www.macupdate.com/info.php/id/11076
    Use it to search your whole drive for Files & Folders, case insensitive & show invisibles...
    Complete
    That will show any with that word in the name at all.

  • Random letters to random numbers in the code template?

    I'd like to change the random letters of the code template in Motion 5 to be random numbers.
    PLATFORM: quad 2.9 macpro, Mavericks, 8 GB RAM, Motion 5.

    type the number you want. I highly recommend a monospace font (or at least a font with monospace number characters -- there are a few...)
    Add > Behaviors > Text Animation
    Parameter Add > Format > Character Offset
    Set Character Offset to 18 (seems to work well.)
    Character Set: Preserve Case & Digits
    Adjust Spread to taste.
    Consider Direction > Random

  • Random numbers in arrays

    i have been faced with a new problem, i have to generate a random number (between 0-63) of random numbers (between 0-63).
    Can i do this all in one array to randomly generate an array containing the random number of elementnts and set each of the elemnt to a random value.
    Random x = new Random();
    int j= x.nextInt(63);
    String [j] numbers;
    Random q = new Random();
    for(j=0, j<=numbers.length, j++)
       int f= q.nextInt(63);
      numbers[j] =f;
    } I had an idea of doing something like the code above can any see a better way to do it.

    I am getting this error
    java.lang.ArrayIndexOutOfBoundsException: 15
    from
    numbers[z] = rng.nextInt(63);i think this is because i am randomly calling the method, so it runs through it once but the second time it flags up the error.
    void Randm()
                         Random rng = new java.security.SecureRandom();
                    int o = rng.nextInt(63);//I hope you know this is zero to 63 exclusive, i.e., it can't return 63
                    int [] numbers = new int[o];
                    for(int z = 0 ; z <= numbers.length ; z++)
                        numbers[z] = rng.nextInt(63);// again, this will not return 63
                        System.out.println("ran gen "+numbers[z]);
    }I think it may be the way i am declaring the array, can anyone shed some light on the situation.

  • Random numbers in Forte V 1.0

    Hi everyone,
    has anyone implemented a method to generate random numbers in Forte Release 1?
    Forte release 2 has just such a utility object, but would love some
    already written code if anyone has it.
    Thanks
    John
    John Jamison
    Engineering Manager
    Sage Solutions, Inc.
    415 392 7243 x 508
    [email protected]

    For everyone's info, there is one on the FSHARE CD that I contributed.
    It is called 'ransort' I believe. It will randomly generate as many
    random numbers as desired, and then provide another option to sort
    them. We no longer need the random generation part in V2, but the
    QuickSort routine is very fast, and worth taking a look at.
    Send me a note if you don't have access to FSHARE, and would prefer
    an export over the net.
    Lee Wei
    >
    Hi everyone,
    has anyone implemented a method to generate random numbers in Forte Release 1?
    Forte release 2 has just such a utility object, but would love some
    already written code if anyone has it.
    Thanks
    John
    John Jamison
    Engineering Manager
    Sage Solutions, Inc.
    415 392 7243 x 508
    [email protected]

  • Random numbers in a table

    I'm new to java, so please bear with me. What I want to do is create a program which chooses random numbers (between 0-9) in a table, and then presents a diagram of how many of each number comes up.
    e.g. if the table size was 10, the output could be something like:
    0 *
    1 **
    2
    3 *
    9 **
    with the stars representing the number of times the number showed up. Any assistance you could provide would be great and appreciated. Thank you

    1) Create a Map. The key will be one of the random numbers, the value will be a counter of the number of times the number has been stored.
    2) Generate a random number.
    3) Place the random number in the map key, and increment the value.
    4)Print the random numbers and as many asterisks as the counter values.
    I believe this is classwork? Asking about problems in the code that are not working is ok - asking people to write sample code for you is not ok. Part of the assignment requires you to read the documentation and figure out how to write the code.

  • How to get the desired total of Random numbers ?

    I m in a big problem and want a urgent solution.
    I want to randomise numbers with specified limit and the total of all the randomised numbers should be desirable.
    e.g. if i m generating random number 24 times in a loop then the total of all the gerenrated numbers should be 100 and each randomised generated number should not be greater than 6.
    Please help me out. is there any method from which i can get, desirable total from the gererated random numbers ?
    Please help.
    thanks in advance

    In my usual manner, I'll throw in a couple of ideas and leave others to discard them again :-)
    Pick random numbers. If, at some point, the sum goes over 100, throw away the last number at instead put in the number that will make the sum exactly 100. Then put in 0 until you have 24 numbers. Those 0s won't look random. If it helps, shuffle all the numbers so the 0 come in random places in the sequence. If it's still not random enough, discard the whole idea. If, on the other hand, you still are m numbers short of 24 numbers and the sum goes under 100 - (m * 6), in a similar manner stop using randomness and throw in the numbers needed to hit 100. Shuffle to distribute the 6s.
    Another idea, there are 100 ^ 24 or 10 ^ 48 ways to put 100 balls into 24 buckets. Find out how many ways there are under the constraint that you can put at most 6 balls into a bucket. Enumerate them, pick one at random and generate it. I would say that this should give you a good randomness, but does it also look that way under your requirements?

  • Random numbers without duplicates

    Hello,
    I'm having trouble locating an explained example for selecting a set of random numbers without duplicates. Specifically, I'm working on this example from Ivor Horton's Beginning Java, chapter 3.
    3. A lottery program requires that you select six different numbers from the integers 1-49. Write a program to do this for you and generate five sets of entries.
    This is not homework, I'm just trying to teach myself Java to eventually work my way up to servlets and JSPs.
    This is what I have so far. I tried to use array sorting but it didn't work (the first three numbers in my entries was always 0), so I commented out that part. I've included sample output after the code. I don't necessarily need the exact code although that would be nice; I just need at least an explanation of how to go about making sure that if a number has been picked for an entry, not to pick it again.
    Thanks for any help,
    javahombre
    package model;
    import java.util.Random;
    import java.lang.Math.*;
    import java.util.Arrays;
    public class Lottery {
    public static void main(String[] args) {
    // Lottery example.
    // We need the following data:
    // - An integer to store the lottery number range (in this case 59).
    // - A mechanism to choose a number randomly from the range.
    // - An array to store the entries (6 different numbers per entry).
    // - A loop to generate 5 sets of entries.
    int numberRange = 59;
    int currentPick = 0;
    int[] entry = new int[6]; // For storing entries.
    //int slot = -1; // For searching entry array.
    Random rn = new Random();
    for (int n = 0; n < 5; n++)
    // Generate entry.
    for (int i = 0; i < 6; i++)
    currentPick = 1 + Math.abs(rn.nextInt()) % numberRange;
    //Arrays.sort(entry);
    //slot = Arrays.binarySearch(entry, currentPick);
    //System.out.println("i: " + i + "\t| slot: " + slot + "\t| pick: " + currentPick + "\t| compare: " + (slot < 0));
    // Add the current pick if it is not already in the entry.
    //if (slot < 0)
    entry[i] = currentPick;
    System.out.println("pick entered: " + currentPick);
    // Output entry.
    System.out.print("Entry " + (n + 1) + ": ");
    for (int j = 0; j < 6; j++)
    System.out.print(entry[j] + "\t");
    System.out.println("");
    // Set the array contents back to 0 for next entry.
    Arrays.fill(entry,0);
    Sample output (notice duplicates, as in Entry 3):
    pick entered: 11
    pick entered: 29
    pick entered: 33
    pick entered: 8
    pick entered: 14
    pick entered: 54
    Entry 1: 11     29     33     8     14     54     
    pick entered: 51
    pick entered: 46
    pick entered: 25
    pick entered: 30
    pick entered: 44
    pick entered: 22
    Entry 2: 51     46     25     30     44     22     
    pick entered: 49
    pick entered: 39
    pick entered: 9
    pick entered: 6
    pick entered: 46
    pick entered: 46
    Entry 3: 49     39     9     6     46     46     
    pick entered: 23
    pick entered: 26
    pick entered: 2
    pick entered: 21
    pick entered: 51
    pick entered: 32
    Entry 4: 23     26     2     21     51     32     
    pick entered: 27
    pick entered: 48
    pick entered: 19
    pick entered: 10
    pick entered: 8
    pick entered: 18
    Entry 5: 27     48     19     10     8     18     

    NOTE: the array reference to [ i ] seems to be misinterpreted as italics by the posting form. I've reposted the message here, hopefully it will show the code properly.
    Thanks again,
    javahombre
    Hello,
    I'm having trouble locating an explained example for
    selecting a set of random numbers without duplicates.
    Specifically, I'm working on this example from Ivor
    Horton's Beginning Java, chapter 3.
    3. A lottery program requires that you select six
    different numbers from the integers 1-49. Write a
    program to do this for you and generate five sets of
    entries.
    This is not homework, I'm just trying to teach myself
    Java to eventually work my way up to servlets and
    JSPs.
    This is what I have so far. I tried to use array
    sorting but it didn't work (the first three numbers in
    my entries was always 0), so I commented out that
    part. I've included sample output after the code. I
    don't necessarily need the exact code although that
    would be nice; I just need at least an explanation of
    how to go about making sure that if a number has been
    picked for an entry, not to pick it again.
    Thanks for any help,
    javahombre
    package model;
    import java.util.Random;
    import java.lang.Math.*;
    import java.util.Arrays;
    public class Lottery {
    public static void main(String[] args) {
    // Lottery example.
    // We need the following data:
    // - An integer to store the lottery number range (in
    this case 59).
    // - A mechanism to choose a number randomly from the
    range.
    // - An array to store the entries (6 different
    numbers per entry).
    // - A loop to generate 5 sets of entries.
    int numberRange = 59;
    int currentPick = 0;
    int[] entry = new int[6]; // For storing entries.
    //int slot = -1; // For searching entry
    array.
    Random rn = new Random();
    for (int n = 0; n < 5; n++)
    // Generate entry.
    for (int i = 0; i < 6; i++)
    currentPick = 1 + Math.abs(rn.nextInt()) %
    numberRange;
    //Arrays.sort(entry);
    //slot = Arrays.binarySearch(entry, currentPick);
    //System.out.println("i: " + i + "\t| slot: " + slot +
    "\t| pick: " + currentPick + "\t| compare: " + (slot <
    0));
    // Add the current pick if it is not already in the
    entry.
    //if (slot < 0)
    entry[ i ] = currentPick;
    System.out.println("pick entered: " + currentPick);
    // Output entry.
    System.out.print("Entry " + (n + 1) + ": ");
    for (int j = 0; j < 6; j++)
    System.out.print(entry[j] + "\t");
    System.out.println("");
    // Set the array contents back to 0 for next entry.
    Arrays.fill(entry,0);
    Sample output (notice duplicates, as in Entry 3):
    pick entered: 11
    pick entered: 29
    pick entered: 33
    pick entered: 8
    pick entered: 14
    pick entered: 54
    Entry 1: 11     29     33     8     14     54
    pick entered: 51
    pick entered: 46
    pick entered: 25
    pick entered: 30
    pick entered: 44
    pick entered: 22
    Entry 2: 51     46     25     30     44     22
    pick entered: 49
    pick entered: 39
    pick entered: 9
    pick entered: 6
    pick entered: 46
    pick entered: 46
    Entry 3: 49     39     9     6     46     46
    pick entered: 23
    pick entered: 26
    pick entered: 2
    pick entered: 21
    pick entered: 51
    pick entered: 32
    Entry 4: 23     26     2     21     51     32
    pick entered: 27
    pick entered: 48
    pick entered: 19
    pick entered: 10
    pick entered: 8
    pick entered: 18
    Entry 5: 27     48     19     10     8     18

  • How to fill an array with random numbers

    Can anybody tell me how I can fill an array with thousand random numbers between 0 and 10000?

    import java.util.Random;
    class random
         static int[] bull(int size) {
              int[] numbers = new int[size];
              Random select = new Random();
              for(int i=0;i<numbers.length;i++) {
    harris:
    for(;;) {
    int trelos=select.nextInt(10000);
    numbers=trelos;
                        for(int j=0;j<i;j++) {
    if(trelos==numbers[j])
    continue harris;
    numbers[i]=trelos;
    break;
    return numbers;
    /*This method fills an array with numbers and there is no possibility two numbers to be the
         same*/
    /*The following method is a simpler method,but it is possible two numbers to be the same*/
    static int[] bull2(int size) {
         int[] numbers = new int[size];
    Random select = new Random();
    for(int i=0;i<numbers.length;i++)
              numbers[i]=select.nextInt(9);
              return numbers;
         public static void main(String[] args) {
    int[] nikos = random.bull(10);
    int[] nikos2 = random.bull2(10);
    for(int i=0;i<nikos.length;i++)
    System.out.println(nikos[i]);
    for(int i=0;i<nikos2.length;i++)
    System.out.println(nikos2[i]);

Maybe you are looking for

  • Same Function module accessible in 4.6c but not in ecc6

    Hi, When I am calling the fm in a program with  parameter which is not defined in FM in SAP ver 4.6 C its not giving any dump, but the same thing when I call in ECC 6 its giving error message that field is not defined. could any one tell me the reaso

  • Error "JRE not in Configuration File"

    I have made sure my browser is Java activated that that all issues in the FAQ are done. I can not find JAVA in my control panel and when I manually execute the java file, I get the error "JRE not in Configuration File." I found some errors in my regi

  • Anyone know how to cancel purchased downloads?

    So I have all these downloads - huge files - that I don't want anymore/ don't want to download... Can I get rid of them permanently without downloading 1st? I keep having to "pause" them every few minutes whenever I'm in iTunes...

  • IPhone: Looking for a 3 level TableView drill down example

    Hi The TableView drill down example available at Apple's Dev Center, as well as all the other examples of drill downs I've seen, only explains how to do a drill down with just 2 levels. The first view is a table view, and when the user selects an ite

  • Sign in problem?profile.language=en

    Hi,This is Reddy from Uganda. I have a problem while signing with my user name and password and not only me here around 50 people are getting same problem,While i am talking in phone its just cutoff and when i am trying to call it showing your accoun