Creating an array of size n with random #'s

can anyone help me? i just need to make an array of size n (exmaple 250) with the data filled in with random #'s instead of consecutive #'s

yes an array of size n shuffledPseudo code
Fill an array of length 'n' with the numbers 0 to n-1 (or 1 to n if this is what you want)
Shuffle the numbers -
Set 'index' to the last element index
while (index > 1)
At random choose an index ( swapIndex ) less than or equal to index.
Swap the values in locations swapIndex and index
Decrement index
}

Similar Messages

  • An array of size 10 with max and min

    I'm been trying to make an array of size 10 that is filled with 10 random numbers. I'm having problems writting a code for finding the largest and the smallest number in the array. Thus the random 10 numbers are inputed by the user and then the program finds the smallest and the largest and then prints those two out.
    here is what i can so far.
    import javax.swing.JOptionPane;
    public class SmallLarge {
    public void main (String [] args) {
    int [] Numbers = new int[2];
    int maxi;
    int mini;
    int i;
    for (i=0; i < 2; i++) {
    String input = JOptionPane.showInputDialog("type in any random number?");
    Numbers[i] = Integer.parseInt(input);
    maxi = Math.max(Numbers, Numbers[i]);
    mini = Math.min(Numbers[i], Numbers[i]);
    System.out.println("Maximum:" + maxi);
    System.out.println("Minimum:" + mini);
    could anyone please help me fix the program?

    however, all i would like to know is how to find the largest and the smallest number from the numbers inputed.
    I don't understand why
    maxi = Math.max(Numbers, Numbers[i]);
    would not work, i believe i must put something else in the ()
    yes, of course I will use the code formatting next time. Now i know. Thank you.

  • ¿How to fill an array with random letters?

    Hello, I was wondering If filling a bidimensional array
    (8x10 for example) with random letters could be done in C#, I've tried tons of stuff, but I can't manage to do it. Thanks in advance.

    I was wondering If filling a bidimensional array
    (8x10 for example) with random letters could be done in C#, I've tried tons of stuff, but I can't manage to do it.
    >I was wondering If filling a bidimensional array (8x10 for example) with random letters
    >could be done in C#,
    Yes.
    >I've tried tons of stuff, but I can't manage to do it.
    With exactly which part of this assignment are you having trouble?
    Can you create a 2-dim array of characters?
    Can you generate a random letter?
    Show the code you have written so far.
    Don't expect others to write a complete solution for you.
    Don't expect others to guess as to which part you're having difficulty with.
    Show the code you have working, and the code which you have tried which isn't working,
    Explain what is happening with it which shouldn't.
     - Wayne

  • How to create an array of indefined elements

    Hi. I need to write a program that store n number of elements in an array. The program have to ask the user to input some values, and it stops until the user says that he dont want to input more values.
    I have the following code:
    public class Controlador{
      private Contacto[] lista;
      private int contactoActual;
      private int continuar = 0;
      private Interfaz interfaz = new Interfaz();
      public Controlador(){
        lista = new Contacto[contactoActual];
        contactoActual++;
      public void insertarContacto(){
        do{
          Contacto persona = new Contacto();
          persona.insertarNombre();
          persona.getNombre();
          persona.getTelefono();
          contactoActual++;
          lista[contactoActual] = persona;
          continuar = interfaz.mostrarContinuar();
        }while(continuar==1);
      }with this, I got an array out of bounds exception.
    Can somebody help?
    regards.

    Yes and no.
    Once you have created an array you cannot change its size. So if you create an array of size 10 and user wants to input 11 numbers, your progrm will crash. Hoever you can get around this by checking the size of the array before inserting. If the array is full, create another array that is bigger, copy all values from original array to new array and then insert the new number. All this requires a lot of work. Use a Collection such as an ArrayList so you don't have to worry about the size.

  • Possible to create an array of devices?

    Hello,
    as a Java Programmer I like to programm in the object orientated manner.
    I have more than 50 valces to manage and established a state diagramm.
    So if the value is true or one in the matrix in row 52 of a column definig the operation type then I would like to set the valve 52 to "open".
    Therefore I would like to put all devices of type valve in an array and start the corresponding valve.at index 52 of a device array in a for loop.
    I have not hardware yet but I probably want to use the DAQ unit of NI.
    Will it be possible to program it in this way?
    Thanks very
     much
    Thommy7571
    Solved!
    Go to Solution.

    Thommy7571 wrote:
    By the way if I create an array and use it with this index component to act on one element this did never work yet..
    Even if the dimensions are equal I get an error:
    error: trying to connect a 2D array of type "boolesch" to 2D array of "invalid". 
    the same if I want to initialize one.
    I don't see any possibility to adapt or define the type of the element
    in an array for all elements at once. How must I do it?
    I will have the same problem with the device array.
    That description makes no sense. Can you show us your code instead?
    LabVIEW Champion . Do more with less code and in less time .

  • How to create an array with Generic type?

    Hi,
    I need to create a typed array T[] from an object array Object[]. This is due to legacy code integration with older collections.
    The method signature is simple:public static <T> T[] toTypedArray(Object[] objects)I tried using multiple implementations and go over them in the debugger. None of them create a typed collection as far as I can tell. The type is always Object[].
    A simple implementation is just to cast the array and return, however this is not so safe.
    What is interesting is that if I create ArrayList<String>, the debugger shows the type of the array in the list as String[].
    If I create ArrayList<T>, the class contains Object[] and not T[].
    I also triedT[] array = (T[]) Array.newInstance(T[].class.getComponentType(), objects.length);And a few other combinations. All work at runtime, create multiple compilation warnings, and none actually creates T[] array at runtime.
    Maybe I am missing something, but Array.newInstace(...) is supposed to create a typed array, and I cannot see any clean way to pass Class<T> into it.T[].class.getComponentType()Returns something based on object and not on T, and T.class is not possible.
    So is there anything really wrong here, or should I simply cast the array and live with the warnings?
    Any help appreciated!

    Ok. May be you could keep information about generic type in the your class:
    public class Util {
        public static <T> T[] toTypedArray(Class<T> cls, Object[] objects){
            int size = objects.length;
            T[] t = (T[]) java.lang.reflect.Array.newInstance(cls, size);
            System.arraycopy(objects, 0, t, 0, size);
            return t;
    public class Sample<T> {
        Class<T> cls;
        T[] array;
        public Sample(Class<T> cls) {
            this.cls = cls;
        public void setArray(Object[] objects){
            array = Util.toTypedArray(cls, objects);
        public T[] getArray(){
            return array;
        public static void main(String[] args) {
            Object[] objects = new Object[] { new LinkedList(), new ArrayList()};
            Sample<List> myClass = new  Sample<List>(List.class);
            myClass.setArray(objects);
            for(List elem: myClass.getArray()){
                System.out.println(elem.getClass().getName());
    }

  • How to create a array with variables dimensions?

    I try to create a array like that:
    Object[][] data;
    data = new Object[] [];
    But that's doesn't work!
    Apparently I must specify the dimension of my array
    So I have done like that :
    Object[][] data;
    data = new Object[3] [3];
    And that work!
    But the problem is when I need to add extra elements to my array.
    If I write :
    data[4][1] = "123";
    I have the error message :
    java.lang.ArrayIndexOutOfBoundsException
    So, how can I defined a array with variables dimensions OR how can I add a dimension to a array?

    if you have:
    Object[][] data;
    data = new Object[3] [3];you end yo getting ArrayIndexOutOfBoundException if you try to point to some other Indexes. You can increase the size by doing new:
    Object[][] data;
    data = new Object[4] [3];and then copy the old Arrays to this one... this is heavy.
    Other thing to consider then is using some other datastructure, such as Vector, which grows along you add elements to it.
    P_trg

  • How to fill array with random number?

    I need to fill a 3-dimensional array that has user-controlled dimension sizes with random numbers (1-10). I'm unsure of how to do this. I feel like I have to use the initialize array and maybe the build array functions somehow but I'm not entirely sure. Any help would be appreciated. Thanks.

    Something like this
    Kudos are always welcome if you got solution to some extent.
    I need my difficulties because they are necessary to enjoy my success.
    --Ranjeet
    Attachments:
    Array with random number.vi ‏9 KB

  • Creating many arrays with different names

    I'm trying to create many arrays with a different names by calling this method:
    n=name of array
    c= some string
    public Array[] newArray(String n, String c)
    n[n.length]= new String();
    n[n.length]= c;
    As you experts might guess this does not compile. Is it even possible to do this?

    no, you cannot make a dynamic variable like that.
    and no, that is not the way to make arrays.
    and no, there is no such thing as "Array[]" ( unless u have made an "Array" object ).
    code for a new array is as such
    public Object[] newArray(int size){
      return new Object[size];
    }called as such:
    String[] strings = (String[]) newArray(10);
    strings[0] = "hmmm";mmmmm

  • How can I create a array with all files from a directory

    How can I create a array of files or varchar with all files from a directory?

    I thought the example could be improved upon. I've posted a solution on my blog that doesn't require writing the directory list to a table. It simply returns it as a nested table of files as a SQL datatype. You can find it here:
    http://maclochlainn.wordpress.com/2008/06/05/how-you-can-read-an-external-directory-list-from-sql/

  • How to create 2D array with 3 rows and unlimit column?

    how to create 2D array with 3 rows and unlimit column?

    Here are images of what I described in my previous post
    Message Edited by JoeLabView on 11-14-2007 07:56 AM
    Attachments:
    2D-array_code.PNG ‏7 KB
    2D-array_values.PNG ‏13 KB

  • How do you create an array of any size(no limits).

    how do you create an array of any size(no limits). this array should hold BigInteger values.
    BigInteger[] array = new BigInteger[100000000]; //creates new array of BigIntegers array[0] = new BigInteger("6"); //puts "6" in
    array[1] = BigInteger.ZERO;
    but this type of an array can only hold a limited amount.

    Use a java.util.List, e.g., ArrayList or LinkedList.

  • How to create an array with controls and indicators?

    I want to create a scrollable array of control/indicator pairs from a config file.  Something that looks like the attached image.  Of course, I can only create an array that's either either a control or indicator -- not both (the operator should not be able to edit the name or change the state of the LED).  So I guess I need to split the array and have two side-by-side and SOMEHOW link the scrollbars.  But then I have no idea how I can get the control switch states from the array.  Is there an easier way to do what I'm trying to do?
    Attachments:
    New Bitmap Image.JPG ‏11 KB

    Hello,
    Another option would be to use the custom control I built for you, see below.  You can just drop this into an array, it should look like your example (more or less).
    Cheers!
    CLA, CLED, CTD,CPI, LabVIEW Champion
    Platinum Alliance Partner
    Senior Engineer
    Using LV 2013, 2012
    Don't forget Kudos for Good Answers, and Mark a solution if your problem is solved.
    Attachments:
    Custom Switch.ctl ‏8 KB

  • How to create an array of ring with a different items/values for each

    Hi All,
    i want an array of text ring with different items and values for each text ring. Do you have other solution if it does not work?
    thanks by advance

    0utlaw wrote:
    Hello Mnemo15,
    The properties of elements in an array are shared across all controls or indicators in an array, so there is no way to specify unique selectable values for different text rings in an array.  It sounds like what you are looking for is a cluster of ring controls, where each control can be modified independently.  
    Could you provide a more descriptive overview of the sort of behavior you are looking for?  Are these ring elements populated at run time?  Will the values be changed dynamically? Will the user interact with them directly?
    Regards,
    But the selection is not a property, it is a value... I just tried it and you can have different selections.  Just not different items.
    Bill
    (Mid-Level minion.)
    My support system ensures that I don't look totally incompetent.
    Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.

  • Out of memory error when creating an array

    I'm trying to create 3D array of Bytes.
    Byte[][][] disk = disk = new Byte[11][9][25344];
    This is where I get an OutofMemory error.
    Please help!!!

    It is not as ballubadshah asserts a matter of how much memory you have but rather how much is allocated by default to the JVM heap.
    This number according to http://java.sun.com/docs/hotspot/ism.html is 64megabytes.
    When I run the following code, I get the number 64618496 which is just shy of 67108864, 64 meg. If I increase the array size I get the same error as you.
    To get past this, you can increase the heap size at runtime with the option -Xmx###m where ### is the number of megabytes to use for heap.
    ex
    c:/test>java -Xmx128m Memorypublic class Memory
       public static void main(String[] args)
          Runtime.getRuntime().gc();
          long start = Runtime.getRuntime().totalMemory();
          Byte[][][] bytes = new Byte[11][9][25344];
          for(int i = 0; i < 11; i++)
             for(int j = 0; j < 9; j++)
                for(int k = 0; k < 45344; k++)
                   bytes[i][j][k] =
                      new Byte(new Double(Math.random() * 256).byteValue());
          Runtime.getRuntime().gc();
          long end = Runtime.getRuntime().totalMemory();
          System.out.println("memory usage  = " + Long.toString(end - start));
    }

Maybe you are looking for