# Methods for generating random array Recommended for you: Get network issues from WhatsUp Gold. Not end users.

As the saying goes, once traversed the road, doing things, always need to wait for a time, perhaps in the dead of night, will think of the left.

Busy for another 7 days, exhaustion of body and mind, but not "the 7 day" so sad; since all survived, you will gain something.

This article will tell you (also be recorded), how to generate a random non repeating array (also can be said to be the list), and of course to fill number is an integer.

How to achieve the following view.:  ``` 1 #include<iostream>
2 #include<algorithm>
3 using   namespace   std;
4
5 int   main()
6 {
7     int   a;
8
9     for(int i=0;i<1000;i++)
10     {
11         a[i] = i;
12     }
13
14     int*   a_begin   =   a;
15     int*   a_end   =   a   +   sizeof(a)/sizeof(int);
16     random_shuffle(a_begin,a_end);
17
18     for(int j=0;   j<1000;   j++)
19     {
20         cout<<a[j]<<"\t";
21     }
22
23     getchar();
24     return 0;
25 }```

Test_Random.cpp

1 first create an array object a[], of course, according to individual needs can also use an array of double or other types of.

2 to the array initialization, the code is for integer cycle directly to a[] elements were given by 1~1000.

3 using random_shuffle Gauss random elements in the array to disrupt the order, thus generating a random array; if you want more chaos, random_shuffle operation can be called many times.

4.Show random array, see the effect.

Question: if and how do I want to generate random sequences of variable length? the following method is feasible.?

```int   main()
{
int* a = null;

a = new int;
for(int i=0;i<1000;i++)
{
a[i] = i;
}
int*   a_begin   =   a;
int*   a_end   =   a   +   sizeof(a)/sizeof(int);
random_shuffle(a_begin,a_end);

for(int j=0;   j<1000;   j++)
{
cout<<a[j]<<"\t";
}

getchar();
return 0;
}
```

Note: friends as code are interested please try it, don't be afraid of friends were shocked！

Solution: Random numbers to generate variable length, correct and feasible scheme is what?  ``` 1 #include<iostream>
2 #include <vector>
3 #include<algorithm>
4 using namespace std;
5
6 int main()
7 {
8     vector<int> a;
9
10     for(int i=0; i<1000; i++)
11     {
12         a.push_back(i);
13     }
14     random_shuffle(a.begin(),a.end());
15
16     for(int j=0; j<a.size(); j++)
17     {
18         cout<<a[j]<<"\t";
19     }
20
21     getchar();
22     if(!a.empty())
23     {
24         a.swap(vector<int>());
25     }
26     return 0;
27 }```

TestRandom2.cpp

1 first create the vector<> object a, of course, according to individual needs can also use double or other types of.

2.for circulation into the 1000 elements, the 1 is the numerical range~1000.

3 using random_shuffle Gauss random elements in the array to disrupt the order, thus generating a random array; if you want more chaos, random_shuffle operation can be called many times.

4.Show random array, see the effect.

At the end of 5 pay attention to clear the vector<>.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Josie at November 17, 2013 - 9:23 PM