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();
}

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,

  • Unique Random numbers

    How to get unique random numbers every time in AS3
    I have searched but not able to get satisfactory results

    var rand :Number;
    rand = Number(movXml.movie.length());
    for(var n:uint = 0 ; n<5 ; n++)
    num1 = Number(Math.round(Math.random()* ((rand-1) - 0) + 0 ) );
    if(tempArray.indexOf(num1) == -1)
    tempArray.push(num1);
    //trace(tempArray);
    num2  = Number (tempArray[0]);
    //trace("num1:"+num2);
    trace(tempArray);
    some time this produce more than 5 numbers
    and some time less than 3
    I want 4 random numbers , but different
    the above loop runs 4 times , so if number is repeated , it will not add to Array , hence i will not able to get 4 Required numbers

  • 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

  • How to generate unique random numbers

    Hi All,
    I am wondering whether there is already a random library or built-in function available in Java to produce some random numbers between certain ranges that are not repetitive. Let's look at a common examples as follows:
    Random diceRoller = new Random();
    for (int i = 0; i < 10; i++) {
      int roll = diceRoller.nextInt(6) + 1;
      System.out.println(roll);
    }My understanding from this approach is that it allows the same number to be repeated over and over again. However, I would like to find out how to continue generating random numbers from remaining ones that haven't been generated earlier.
    Using the above example to illustrate my intention:
    1st random number generated - possibility of 1 - 6 showed up. Say 5 is picked.
    2nd random number generated - possibility of 1, 2, 3, 4, 6 only. Say 2 is picked.
    3rd random number generated - possibility of 1, 3, 4, 6 available. Say 1 is picked.
    4th random number generated - possibility of 3, 4, 6 left. Say 6 is picked.
    5th random number generated - possibility of 3, 4 remains. Say 4 is picked.
    Any assistance would be much appreciated.
    Many thanks,
    Jack

    htran_888 wrote:
    Hi All,
    I am wondering whether there is already a random library or built-in function available in Java to produce some random numbers between certain ranges that are not repetitive. Let's look at a common examples as follows:
    Random diceRoller = new Random();
    for (int i = 0; i < 10; i++) {
    int roll = diceRoller.nextInt(6) + 1;
    System.out.println(roll);
    }My understanding from this approach is that it allows the same number to be repeated over and over again. However, I would like to find out how to continue generating random numbers from remaining ones that haven't been generated earlier.
    Using the above example to illustrate my intention:
    1st random number generated - possibility of 1 - 6 showed up. Say 5 is picked.
    2nd random number generated - possibility of 1, 2, 3, 4, 6 only. Say 2 is picked.
    3rd random number generated - possibility of 1, 3, 4, 6 available. Say 1 is picked.
    4th random number generated - possibility of 3, 4, 6 left. Say 6 is picked.
    5th random number generated - possibility of 3, 4 remains. Say 4 is picked.
    Any assistance would be much appreciated.If it is your school assignment then you have the answer above (List & the lists length).
    (You might want to look at Collections)

  • 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]);

  • 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

  • Need help with random numbers

    hi i need to generate 2 random numbers from array list . and then take out that two numbers from list.
         String[] plcard = { "AC", "KC", "QC", "JC",
              "10C", "9C", "8C", "7C","6C","5C","4C","3C","2C", "AD", "KD", "QD", "JD",
              "10D", "9D", "8D", "7D","6D","5D","4D","3D","2D", "AS", "KS", "QS", "JS",
              "10S", "9S", "8S", "7S","6S","5S","4S","3S","2S", "AH", "KH", "QH", "JH",
              "10H", "9H", "8H", "7H","6H","5H","4H","3H","2H",};
    this is the list if someone can help me i would appreciate
    thanks in advance

    haha, never noticed the .shuffle(List) method!
    maybe java is getting too convenient (just kidding)?
    i never wrote a card game.
    how would the more programmingly gifted of us do this?
    this is my shot at it for what its worth...
    public class Card{
    public Card(int rank, int suit){
    this.rank = rank;
    this.suit = suit;
    int rank (or String i suppose)
    int suit;
    static final club = 1;
    static final spade = 2;
    static final heart = 3;
    static final diamond = 4;
    public class Shuffler{
    public Shuffler(){
    int NumOfDecks = 1;
    Vector ShuffledDeck;
    // num of decks
    for(int i = 0; i < NumOfDecks){
    // 4 suits
    for(int j = 0; j < 4; j++){
    // 13 ranks
    for(int k = 0; k < 13; k++){
    ShuffledDeck.add(new Card(k, j));
    } // suits
    } // num of decks
    Collections.shuffle(ShuffledDeck);
    // Done?
    }

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

  • 100 Random, 2-digit, array elements.... help!!!!

    Hello. I need to populate an array with 100 random 2-digit elements, then, in a regular output window, print those elements on 10 separate lines, 10 elements per line.... After about 3-4 hours of failures with 'for', 'while', and 'if' statements, I admitted to myself that I am stumped. If anyone could help me out on this within the next 14-16 hours, I would greatly appreciate it.

    here's what i've gotten so far:
    import java.util.Random;
    public class ArrayPopulation
    final static int LIMIT = 10;
    public static void main (String args [])
         Random generator = new Random ();
         int index = 0, index2 = 0;
         while (index < LIMIT)
              int random = generator.nextInt(99);
              if(random >= 10)
                      int[] TestScores = new int[random];
    System.out.print (TestScores[index] + "
    index] + "  ");
                       if (index > LIMIT && index2 < LIMIT)
    System.out.println();
    index2++;
              index++;
    }my out put is always 0's for this, and i lost track
    of the method that gave me numbers. unfortunately, it
    is not printing 10 elements per line, not to mention
    10 lines with 10 elements each line (totalling 100
    elements). i'm going crazy because it is probobly
    something small, but i really don't know at this
    point.Well, one of the first problems you are having is that you are creating a new int array every time you execute the following line:
    int[] TestScores = new int[random]The size of the new array is random and it is probably initializing to have all zeros in the array, which is why you print zeros.
    You probably want to assign the value into an array, and keep the array around until it is populated. Then print it.

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

  • 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));

  • Help Sorting array of strings with numbers in them

    Hello I need to sort an array of Strings of numbers. The order is not what I want it to be. instead of 1, 5, 20 it will go 1, 20, 5.
    I did a google search for this and found some code that could fix this. The only problem is I tried using it as a method in my program and am getting a compile-time error that I'm not sure how to fix.
    C:\Documents and Settings\Owner\Desktop\stringcompare.java:18: '(' or '[' expected
                new Comparator<String>()
                                    ^I'm not sure what to do here. Any help would appreciated.

    import java.io.*;
    class MyProgram{
    //This is my old bubble sort method which screws up with numbers
    public static String[] sort( String[] points)
              boolean keepGoing = true;
              while(keepGoing == true){
                   keepGoing = false;
                   for(int i = 0; i < points.length - 1; i ++)
                        if(points.compareToIgnoreCase(points[i + 1]) < 0)
                             String temp = points[i];
                             points[i] = points[i + 1];
                             points[i + 1] = temp;
                             keepGoing = true;
              return points;
    //This method reads a pre-existing text file
    public static String[] readPoints() throws IOException
              FileReader file = new FileReader("points.txt");     
              BufferedReader in = new BufferedReader(file);
              String temp;                
              String[] list = new String[100];     
              int i = 0;
              while (i < list.length)                    
                   temp = in.readLine();
                   if (temp != null)
                        list[i] = temp;                    
                   else
                        list[i] = " ";
                   i++;
              in.close();                              
              return list;
    //rewrites file after being sorted
         public static void savePoints(String[] points) throws IOException
              FileWriter file = new FileWriter("points.txt");
              BufferedWriter out = new BufferedWriter(file);
              int i = 0;
              while (i < points.length)          // while the end of file is not reached
                   out.write(points[i]) ;
                   out.newLine();
                   i++;
              out.flush();
              out.close();
    public static void main(String[]args) throws Exception{
    String[]points = new String [100];
    points = readPoints();
    points = sort(points);
    savePoints(points);
    for(int i = 0; i < points.length - 1; i++)
    System.out.println(points[i]);
    The problem is that it doesn't sort as I would like it and I'm not sure how to get the comparator to work properly.
    Edited by: myol on May 31, 2008 4:21 PM
    Edited by: myol on May 31, 2008 4:23 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

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

Maybe you are looking for

  • Tablet Only Showing Blank Screen

    My tablet is not showing anything on the screen. Only the backlight is on, so the screen gets power. Pressing Windows button is vibrates the tablet. Holding the power button for 10 seconds shuts it off. A short press (like waking it up) lights the bl

  • Special usb cable for Nokia 5230?

    Does the Nokia 5230 use a special usb cable? I can't get my ordinary usb cable to fit. (And the secondary question if yes on the first would be why? Out if greed?) Solved! Go to Solution.

  • Is it allowed to make a rmi call from within an ejb??

    Hello, Am I allowed to make a rmi call from within an ejb such as: String host = "localhost"; RemoteObject ro = (RemoteObject) Naming.lookup("rmi://"+host+"/MonServeur"); ro.myMethod(); package pack; import java.rmi.Naming; import java.rmi.RemoteExce

  • Compton + Dwm: Shadow as semi-transparent border?

    Hi! http://i.imgur.com/XQME18jl.png I ran into this image while derping on the internet and I've been trying to get borders/shadows like in that picture for a some time. I know that this is achieved using Compton and Dwm, but I can't find a way how t

  • ISE Alarm Settings

    I'm looking for a way to control the alarm settings a little more granularly.  Currently when I modify the SMTP settings and alarm settings I end up getting every alarm sent to the email address I specify.  The problem is, I don't want ever single al