Randomization of sequence elements using the random_shuffle () algorithm

Source: Internet
Author: User
Tags integer numbers range

Suppose you need to specify a range of random numbers, the traditional method is to use the ANSI C function random (), and then format the result so that the result falls within the specified range. However, there are at least two drawbacks to using this method. First of all, when formatting, the result is often distorted, so the correct random number is not obtained (for example, some numbers are more frequent than others). Second, random () only supports integer numbers; it cannot be used to produce random characters, floating-point numbers, strings, or records in a database.

For the above two problems, C + + provides a better solution, that is the Random_shuffle () algorithm. Don't worry, I'll tell you how to use this algorithm to produce random numbers of different types.

The best way to produce a set of random elements within a specified range is to create a sequential sequence (that is, a vector or a built-in array) that contains all the values of a specified range in that sequence. For example, how do you need to produce a number between 0-99 and 100, then create a vector and fill the vector with 100 numbers in ascending order:

#include <vector>
using std::vector;
int main()
{
 vector<int> vi;
 for (int i = 0; i < 10; i++)
  vi.push_back(i);
/*现在向量包含了 100 个 0-99 之间的整数并且按升序排列*/
}

After filling the vector, the random_shuffle () algorithm is used to disrupt the order of elements. Random_shuffle () is defined in the standard header file <algorithm.h>. Because all STL algorithms are declared in the name Space std::, you should pay attention to declaring the data type correctly. Random_shuffle () has two parameters, the first argument is an iterator that points to the first element of the sequence, and the second argument points to the next position in the last element of the sequence. The following code snippet uses the Random_shuffle () algorithm to disrupt elements previously populated into vectors:

#include <algorithm>
using std::random_shuffle;
random_shuffle(vi.begin(), vi.end()); /* 打乱元素 */

If you want to check for disturbed elements, you can look at the order in which they were stored after they were upset:

for (int i = 0; i < 100; i++)
 cout<<vi[i]; /* 显示被打乱顺序的元素 */

Random_shuffle () is a fully generic algorithm-suitable for built-in data types and user-defined types. The following example creates a vector with 7 string objects that contains the number of days of the week and uses random_shuffle () to disrupt their ordering:

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
 vector<string> vs;
 vs.push_back(string ("Sunday"));
 vs.push_back (string ("Monday"));
 ...
 vs.push_back (string ("Saturday"));
 random_shuffle(vs.begin(), vs.end()); /* 打乱顺序 */
 for (int i = 0; i << 7; i++)
  cout<<vs[i]; /* 显示打乱顺序后的元素 */
}

How to use Random_shuffle () to process a built-in array

When using a container instead of a built-in array, you don't have to bear any burdens. All STL algorithms apply not only to containers, but also to sequences. Therefore, you can also apply the random_shuffle () algorithm to the built-in array. Just note that the second argument to Random_shuffle () points to the next element position on the upper bound of the array:

char carr[4] = {''a'', ''b'', ''c'', ''d''};
/*carr+4 指向数组上界的下一个元素位置*/
random_shuffle(carr, carr+4); 
for (int i = 0; i < 4; i++)
 cout<<carr[i]; /* 显示被打乱顺序的元素 */

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.