Random numbers without using java funtions

i need a formula to generate 100 random numbers 1-20
any suggestions?

Doesn't need much explaining. Did you try it?
http://www.google.com/search?q=RNG+algorithms&sourceid=mozilla-search&start=0&start=0&ie=utf-8&oe=utf-8

Similar Messages

  • 6 random numbers without repeat

    I'm trying to make a 6 digit random number generator between 1 and 48. When the user click a button 6 random numbers show up in 6 different textFeilds. The problem is that I don't want the same number twice. How can I generate 6 different random numbers without using return and breaking out of the function?
    import flash.events.Event;
    stop();
    btn.addEventListener (MouseEvent.CLICK, random1);
    function random1 (evt:Event) {
              display1.text = "";
              display2.text = "";
              display3.text = "";
              display4.text = "";
              display5.text = "";
              display6.text = "";
              var r1 = Math.floor(Math.random()*(1+48-1))+1;
              var r2 = Math.floor(Math.random()*(1+48-1))+1;
              var r3 = Math.floor(Math.random()*(1+48-1))+1;
              var r4 = Math.floor(Math.random()*(1+48-1))+1;
              var r5 = Math.floor(Math.random()*(1+48-1))+1;
              var r6 = Math.floor(Math.random()*(1+48-1))+1;
              if (r2 == r1 || r3 == r2 || r4 == r3 || r5 == r4 || r6 == r5) {
                        return;
              var liste:Array = new Array();
              liste.push(r1,r2,r3,r4,r5,r6);
              liste.sort(Array.NUMERIC);
              display1.text = String(liste[0]);
              display2.text = String(liste[1]);
              display3.text = String(liste[2]);
              display4.text = String(liste[3]);
              display5.text = String(liste[4]);
              display6.text = String(liste[5]);

    Here's the code for the approach mentioned with your textfields included
    btn.addEventListener (MouseEvent.CLICK, random1);
    function random1 (evt:Event) {
        var nums:Array = new Array();
        // fill the array of numbers
        for(var i:uint=1; i<=48; i++){
            nums.push(i);
        shuffle(nums);
        // grab the first six in the shuffled array
        var liste:Array = nums.splice(0,6);
        // assign the values to the textfields
        for(var j:uint=1; j<=6; j++){
            this["display"+String(j)].text = String(liste[j]);
    function shuffle(a:Array) {
        var p:int;
        var t:*;
        var ivar:int;
        for (ivar = a.length-1; ivar>=0; ivar--) {
            p=Math.floor((ivar+1)*Math.random());
            t = a[ivar];
            a[ivar] = a[p];
            a[p] = t;

  • 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

  • Performing sql queries in java without using java libraries

    i wonder whether its possible to perform sql queries beginning from create table to updating queries without using the java sql library.
    has anyone written such code.

    You could use JNI to talk to a native driver like the Oracle OCI driver. Doing this is either exiting or asking for trouble depending on your attitude to lots of low level bugs.

  • How to upload the file without using java components?

    Hello,
    I need to upload the file using JSP, but I don't want to use java componets(such as
    jspsmart).
    Who can tell me how to realize it? This issue is very important to me,
    thanks all in advance!

    make you're own servlet using the multipartrequest from o'reilly.
    easy and simple, no jspsmart, only 1 jar file and some descent programming.
    I use it like that...

  • How we close Views without using Java Script

    Hi All,
    can you please suggest me how i close view with out using java script.
    if possible please reply with code.
    Thanks
    Siva

    Hi Siva,
    You can do this either by hiding your window or by closing the window. Following is the sample code to do that
    IWDWindowInfo windowInfo = null;
    IWDWindow window =null;
    windowInfo = (IWDWindowInfo)  wdComponentAPI.getComponentInfo).findInWindows("<<WindowName>>");
    if(windowInfo!=null)
    window = wdComponentAPI.getWindowManager().createWindow(windowInfo,true);
    window.hide();
    Try with this code, might be useful for your scenario.
    Thanks,
    Sandeep

  • How to write excel file (.xlsx) using file adapter without using java code

    Hi All,
    In soa suite 11g Is there any ways to write the data to the excel ( xlsx ) file using fileadapter and not using java code..Thanks in Advance

    Hi Siva,
    I don't think there is any way to write .xls file directly. You'll have to use some Java API (iText etc.) to create an .xls file. However, you can write .csv file that can be easily converted into .xls at the target end. In MS, it opens as Excel file if delimiter is comma *,*.
    Regards,
    Neeraj Sehgal

  • How to implement multi threading without using Java's synchronization

    I have a shared Object called "tradeObj" whose attributes are "Price" and "Quantity".
    Thread1 is going to change the price and Thread2 is going to change the quantity but not stepping on each other's update.
    Because of the cost of synchronization, I dont want to use "Synchronize", but still want to make sure both threads make their update in a proper manner.
    Any ideas?

    Soph wrote:
    I have a shared Object called "tradeObj" whose attributes are "Price" and "Quantity".
    Thread1 is going to change the price and Thread2 is going to change the quantity but not stepping on each other's update.given above I would guess [your interviewer|http://forums.sun.com/thread.jspa?messageID=10976469#10976469|interview] had in mind reducing locks contention (google for "java concurrency locks granularity" or "java concurrency locks contention" if you're interested)
    // disclaimer: I didn't test this code with compiler
    class TradeObj {
      private int price, quantity;
      // locks
      private final Object priceLock = new Object();
      private final Object quantityLock = new Object();
      public int getPrice() {
        synchronized (priceLock) { return price; }
      public void setPrice(int price) {
        synchronized (priceLock) { this.price = price; }
      public int getQuantity() {
        synchronized (quantityLock) { return quantity; }
      public void setQuantity(int quantity) {
        synchronized (quantityLock) { this.quantity = quantity; }
    Because of the cost of synchronization, I dont want to use "Synchronize", but still want to make sure both threads make their update in a proper manner.what you want above doesn't make much sense to me sorry
    Any ideas?Edited by: gnat on Apr 23, 2010 9:44 AM

  • How Generating Series Of Numbers Without  use Sequences

    i wana use insert table
    i use
    insert table_name (id)
    vaule (( SELECT ROWNUM N FROM dual CONNECT BY LEVEL <= 20))
    but error ORA-01427: single-row subquery returns more than one row

    ali_alkomi wrote:
    ORA-01427: single-row subquery returns more than one row
    insert into FISH_FAMILIES(SCIENCE_NAME,ARABIC_NAME,LATIN_NAME,IS_FAMILY)
    values ( (select distinct FAMILY from FISH_SPECIES) ,(select ARABIC_DESCRIPTION from FISH_SPECIES) ,(select LATIN_DESCRIPTION from FISH_SPECIES),(select distinct IS_FAMILY from FISH_SPECIES
    ))STOP POSTING YOUR SAME QUESTION ON DIFFERENT THREADS!
    how use insert more than query (select)
    If you continue posting the same on both threads, I will lock or delete one of them.

  • How do I generate random numbers from a list of numbers without repeating any number

    I am trying to generate a list of random numbers without any repeating numbers.  For example say the list is from 1 to 15, how do I randomly generate a list of numbers using each number only once?

    pb,
    You can build a randomizer by making a 2-column table with 15 rows. In Column A, Fill with the numbers 1 to 15. In column B Fill with RAND(). Then sort on Column B. There will now be a randomized list of the numbers from 1 to 15 in Column A. You can copy this random list and use it in your application.
    Jerry

  • Create a congruential random numbers

    I want to create a congruential random numbers generator in java:
    Below is the formula for it.
    ri+1=(a*ri+b) mod M .
    How can I use Math class for this?

    Your guess w.r.t. a and b being constants is right. An ordinary multiple congruence random number
    generator simply looks as you described:
    x[n+1]= (a*x[n]+b)%M
    But there's a bit more to it: a and b have to be relatively prime, i.e. gcd(a, b) == 1 and the same counts
    for both a and b w.r.t. M, i.e. gcd(a, M) == 1 and gcd(b, M) == 1. Only then the 'cycle length' of your
    generator will be maximal. In practice M is a power of two (2^32 comes to mind). Here's some code:public class MGRNG {
       private int a, b, M;
       private int seed;
       // ctors
       MGRNG(int a, int b, int M) { this(a, b, M, 1); }
       MGRNG(int a, int b, int M, int seed) {
          this.a= a;
          this.b= b;
          this.M= M;
          this.seed= seed;
       // next pseudo random number
       public int next() {
          seed= (a*seed+b)%M;
          return seed;
    } kind regards,
    Jos

  • How can I generate multiple unique random numbers?

    Hello,
    I am trying to generate multiple random numbers between a given set of numbers (1-52) and not have the same number generated twice within that set. I can compare the last and next numbers with the compare function but how would I go about comparing all of the generated numbers without using a huge list of shift registers...
    Any help/ideas are welcome and appreciated.
    Jason
    Solved!
    Go to Solution.

    Here is an implementation of Jason's solution.
    steve
    Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
    question. Give "Kudos" to replies that help.
    Attachments:
    random.vi ‏10 KB

  • Is it possible to develop struts application without using JPF in workshop 8.1?

    Hi,
    Is it possible to develop application based on struts framework without using
    Java Page Flow in workshop 8.1?
    I developed one application without using JPF , but it is showing some Deployment
    errors.
    rgds,
    girish

    Girish--
    Sure, though, I'd still encourage you to take a look at JPF. :)
    If you remove the JPF runtime files from WEB-INF/, WEB-INF/lib, and JPF specific entries in
    web.xml, you can start defining a struts-config.xml file against the Struts 1.1 runtime in the
    webapp, or you can replace that with a different version as you need. You'll also need to define
    the Struts runtime in web.xml including the servlet, taglibraries, etc.
    One note, the JPF flow view and other JPF specific editors don't work over a normal
    struts-config.xml file.
    Hope that helps...
    Eddie
    Girish wrote:
    Hi,
    Is it possible to develop application based on struts framework without using
    Java Page Flow in workshop 8.1?
    I developed one application without using JPF , but it is showing some Deployment
    errors.
    rgds,
    girish

  • Download and execute jars without using javaws

    Hi
    I need to download and execute jars from remote computer. I hava jnlp file, but I strongly need to execute jars without using java web start.
    What is the best way to do this task?
    Thanks in advance :)
    Edited by: joekidd on Aug 3, 2009 5:55 AM

    joekidd wrote:
    The easiest the best:)The easiest way is to hire the programmers who made the Jar, to consult on your behalf to install the program, or advise on its installation. BTW - you said earlier.. "strongly need to execute jars without using java web start." Why? What advantage does that provide?
    So I download needed jars, add them to classpath and run my application (I get all information from jnlp file and pass them to vm and my application).
    But following exception occurs "Exception in thread "main" java.lang.NoClassDefFoundError: javax/jnlp/ServiceManager". What should I do?Get in contact with those programmers, because this app. is dependant(1) on functionality that is only available to web start apps.
    (1) Unless they put that call in there, just to ensure no one can run the app. outside web start. ;-)

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

Maybe you are looking for