Usage of the rand () function in C + +

Source: Internet
Author: User
Tags random seed

C + + usage of the rand () function in   

2011-12-30 11:03:59| Category: c/c++| Report | font size Subscription

One, C + + cannot use the random () function

The random function is not an ANSI C standard and cannot be compiled under a compiler such as GCC,VC. However, in the C language, the int random (num) can be used in this way, which returns a random number from 0 to num-1. You can use the RAND function under C + + to implement it.

1. The C + + standard function library provides a random number generator RAND, which returns a pseudo-random integer evenly distributed between 0-rand_max. The Rand_max must be at least 32767. The rand () function does not accept parameters and defaults to 1 as the seed (that is, the starting value). The random number generator always starts with the same seed, so the pseudo-random sequence is the same and loses its random meaning. (But this is easy for the program to debug)

2, another function Srand () in C + +, you can specify a different number (unsigned integer variable) to seed. But if the seed is the same, the pseudo-random sequence is the same. One way is to let the user enter the seed, but still not ideal.

3, the ideal is to use the number of changes, such as time to be a random number generator seed. The value of time is different every moment. So the seed is different, so the random number produced is different.

C + + random function (VC program)

#include <stdio.h>

#include <iostream>

#include <time.h>

using namespace Std;

#define MAX 100

int main (int argc, char* argv[])

{

Srand ((unsigned) time (NULL)); the//srand () function produces a random seed that starts at the current moment. It should be in front of the for-waiting loop, or it'll take a long time to wait.

for (int i=0;i<10;i++)

Cout<<rand ()%max<<endl;//max is the maximum value, and its random field is 0~max-1

return 0;

}

Ii. usage of rand ()

RAND () does not require arguments, it returns an arbitrary integer from 0 to the maximum random number, and the maximum random number size is usually a fixed large integer.

/* Maximum value returned by "Rand" function

*/

#define RAND_MAX 0x7fffu

This is the definition in Bcc55, which indicates that the maximum number of integers is 0x7fffu,u for Unicode encoding.

This way, if you want to produce a 0~10 of 10 integers, you can express it as:

int N = rand ()% 11;

Thus, the value of n is a random number of 0~10, and if it is to produce 1~10, this is the case:

int N = 1 + rand ()% 10;

In summary, it can be expressed as:

A + rand ()% n

Where a is the starting value and N is the range of integers.

A + rand ()% (b-a+1) represents a random number between a~b

To 0~1 a decimal number, you can get the 0~10 integer, then divide by 10 to obtain a random decimal number to a very bit, and to get a random decimal number that is random to the percentile, you need to get the 10 integers of 0~100, then divide by 100, and other cases depend on

This analogy.

Usually rand () generates random numbers that are the same as the last time they were run, and this is intentionally designed to facilitate debugging of the program. To produce a different random number each time, you can use the Srand (seed) function to randomize, and with different seed, you can produce different random numbers.

As you may say, you can also include the time.h header file, and then use Srand (Time (0)) to randomize the random number generator using the current times, which guarantees that a different sequence of random numbers can be obtained every two runs (as long as the two runs are more than 1 seconds apart).

This article from CSDN Blog: http://blog.csdn.net/woxueliuyun/archive/2008/02/29/2132543.aspx

#include "stdio.h"

#include <stdlib.h>

#include "Windows.h"

#include <conio.h>

#define RAND_MAX 0X7FFF

void Main ()

{int m=60,n=10,ans=0;

Srand ((unsigned) getcurrenttime ()); Add this to the upright random, the header file is Windows.h

for (int i=0;i<200;i++)

{

printf ("%4d", Rand () * (m-n)/rand_max+n);//n<=x<m number

}

Getch ();

}

Use the C + + random function rand () to generate n numbers, using the bubbling sort method. The two methods of sorting the n number are ordered by using the function.

[Tags: c + +, random function, Rand] green coir raincoat (Lian answer: 1 popularity: 1 fix time: 2010-12-23 09:24

Satisfaction Answer rating: 100% #include <iostream>

#include <string>

#include <ctime>

using namespace Std;

void Maopao_sort (int array[], int n)

{//Bubble sort

int tmp;

for (int i = 0; i < n-1; i++)

{

for (int j = 0; J < N-i-1; J + +)

{

if (Array[j] < array[j+1])

{

TMP = array[j+1];

ARRAY[J+1] =array[j];

ARRAY[J] = tmp;

}

}

}

}

void Select_sort (int array[],int N)

{//select sort

int small;//TEMP Variable Register

for (int i=0;i<n-1;i++)

{

small = i;

for (int j=i+1;j<n;j++)

{

if (Array[small] > Array[j])

{

small = j;

}

}

if (small!=i)

{

int t = Array[small];

Array[small]=array[i];

array[i]=t;

}

}

}

void Main ()

{

int num_ary[10];

cout << "original array Order:" << Endl;

Srand ((unsigned int) time (0));

for (int i = 0; i < sizeof (num_ary)/4; i++)

{

Num_ary[i] = rand ()%50;//the number between random 50 to initialize the array num_ary

cout << Num_ary[i] << Endl;

}

Select_sort (num_ary, sizeof (num_ary)/4);//select sort from small to large

cout << "Select sort from small to large:" << Endl;

for (int i = 0; i < sizeof (num_ary)/4; i++)

{

cout << Num_ary[i] << ",";

}

cout << Endl;

Maopao_sort (num_ary, sizeof (num_ary)/4);//bubble sort from big to small

cout << "bubble sort from big to small:" << Endl;

for (int i = 0; i < sizeof (num_ary)/4; i++)

{

cout << Num_ary[i] << ",";

}

}

How to generate 1-10 10 different random numbers in C + + anonymous answer: 2 Popularity: 2 resolution time: 2011-03-12 15:11

Satisfaction Answer Rating: 100% with the rand () function, you first see how rand () is used, notice that it is pseudo-random, initialize the seed to be set up, on-line to find out the information more than want to know. Then, you want a different 10 number. You can set up a number of groups to store the numbers that have been generated. For example, you have generated the first number 5, you use RAND () to generate another 5 o'clock, compare the resulting number of the discovery is 5, skip, look at the next. Always produces 10 different random numbers.

The disadvantage is rand () is not good enough, and the algorithm is not optimized, but can meet your requirements, on the 10 number, does not affect efficiency. If you want tens of thousands of different, you need to optimize the algorithm.

Evaluate the answer

You've already rated it! Good: 5 You've already reviewed it! Bad: 0 You've already rated it! Original: 5 you have already evaluated! Non-Original: 0 Park answer Acceptance Rate: 10.5% 2011-03-11 19:18 satisfactory answer rating: 100%

The algorithm is that each generation of a number compared to the original generated number, if there is equal to regenerate a number, so that the 10 numbers are not equal. The following program generates numbers ranging from 0 to 100:

#include <iostream>

#include <stdlib.h>

#include <time.h>

#define MAX 100using namespace std;

int main ()

{int arr[10];

int i=0; BOOL Ret=false;

Srand ((unsigned) time (NULL));

while (1) {Arr[i]=rand ()%max+1;

for (j=0;j<i;j++) {if (Arr[j]==arr[i]) {ret=true;      }} if (!ret) {i++;    Ret=false;    } if (i==10) {break;  }} while (--) {cout<<arr[i]<< ""; }

Usage of the rand () function in C + +

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.