An algorithm that produces a non-repeating random array (random array of elements)

Source: Internet
Author: User

Produces an unordered array of length 100, consisting of a number from 0 to 99, with different elements.

In other words, a continuous array of 0 to 100 is scattered.

by three different ways.

One: Using list and random numbers

  long s1 = system.currenttimemillis ();  //  list ar =  new arraylist ();  //   for  (int i = 0; i  < 100000; i++)  {//    ar.add (i);//   } //    int [] toArray = new int[100000];//   //    for (int j = 100000; j >0; j--) {//     double d   =  (Math.random () *j);//     int index =  (int) d;//      toArray[j-1]=  (int) ar.get (index);//     ar.remove ( Ar.get (index));//   } //   long s2 =  System.currenttimemillis ();  //   system.out.println ("S2-s1 spents:" + (S2&NBSP;-&NBSP;S1));  s2-s1:14881

Two. Create an unordered array by swapping the position of the original array element instead of removing the elements from the original collection

   long s1 = system.currenttimemillis ();      int [ ] ar = new int[100];   for  (int i = 0; i <  100; i++)  {       ar[i]=i;   }     int [] toarray = new int[100];    for (int j  = 100; j >0; j--) {     int index  =  (int) (Math.random () *j);          toarray[j-1]= ar[index];      int temp  = ar[index];     ar[index ] = ar[j-1];     ar[j-1] = temp;   }    long s2 = system.currenttimemillis ();    system.out.println ("S2-S1:" + (s2 &NBSP;-&NBSP;S1));

Three: Define a switch for each element of the original array, determine whether the element repeats through the switch, and if the repetition continues to generate a random number, know not to repeat

  long s1 = system.currenttimemillis ();    //counters   tags How many times is this method circulating?    int c = 0;     //This is 0-99 number   int[] iqs  = new int[100000];  for  (int i = 0; i < 100000 ;  i++)  {   iqs[i] = i;  }   //This is a random array    int[] sb = new int[100000];  //Setting Switch   boolean[] b = new  boolean[100000];  //Assignment   for  (int i = 0; i <  100000; i++)  { //  traversing IQs array    int index;   do {     c++;    index =  (int)   (math.random ()  *  iqs.length);  // iqs array random subscript    } while  (b[index] == true);  //  b[index] generates a corresponding character for true code, the subscript is rebuilt        sb[i] = iqs[index]; //  Remove the elements from the IQs to each element of SB        b[index] = true; //  True indicates that   }     system.out.println (c) has been generated;    long s2  = system.currenttimemillis ();    system.out.println (S2&NBSP;-&NBSP;S1);   return sb;

This article is from the "Java Stuff" blog, so be sure to keep this source http://1027187712.blog.51cto.com/5509347/1628020

An algorithm that produces a non-repeating random array (random array of elements)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.