Using system functions to generate random numbers

Source: Internet
Author: User
Tags random seed

The rand () function produces a random number of 0 to Rand_max (32767), with unsigned int double byte is 65535, and four bytes is an integer range of 4294967295.

0~rand_max the probability of each number being selected is the same. The RAND function is internally implemented using linear congruence, which is not a true random number because the periodic

Don't be long, so it can be seen as random.  When the user does not set a random seed, the system default random seed is 1. Rand generates pseudo-random numbers, each time it is executed as a phase

The same, if you want to use random seed function Srand initialization, if the program generates random numbers continuously, it is possible to use srand or there will be a lot of phase

With random numbers, even if the direct use time ((unsigned) times (NULL)) is not resolved as a random seed, it is possible to set the time as a random seed merged

The random number is then used as a random seed for the next generation of random numbers. The complete code used below is available.

static int seed = (unsigned) time (NULL);
int randomonenum (int min,int max,bool bnegative)//Generate a random number
{
if (min <= 0 | | Max < 0 | | min >= max)
{
return 0;
}
Srand (seed);
Seed = rand ();

int num = rand ()% (max+1-min) + min;
if (!bnegative)
{
return num;
}
Return-num;
}

int* randomnums (int min,int max,int n,bool bnegative)//Generate random numbers within the specified range, can have duplicates, can choose to produce negative numbers, positive negative numbers do only one range
{
if (min <= 0 | | Max < 0 | | min >= Max | | n < 0)
{
return NULL;
}

int *pnums = new Int[n];
if (NULL = = pnums)
{
return NULL;
}

for (int i = 0; i < n;i++)
{
Pnums[i] = Randomonenum (min,max,bnegative);
}

return pnums;
}

int* randomnodumpnums (int min,int max,int n,bool bnegative)//generates random numbers within a specified range, cannot be duplicated, can choose to produce negative numbers, and uses set unique attributes to implement

{
if (min <= 0 | | Max < 0 | | min >= Max | | n < 0)
{
return NULL;
}

int *pnums = new Int[n];
if (NULL = = pnums)
{
return NULL;
}

    std::set<int> set_nums;
    int nIndex = 0;
    while (true)
    {
        if (set_ Nums.size () = = N)
        {
             break;
       }
        std::p air<std::set<int>::iterator,bool> Insert_pair;
        Insert_pair = Set_nums.insert (Randomonenum (min,max,bnegative));
        if (Insert_pair.second = = true)
         {
            pnums[nindex++] = *insert_ Pair.first;
       }
   }
    set_nums.clear ();

return pnums;
}

int* RandomNoDumpNums2 (int min,int max,int n,bool bnegative)//generates random numbers within a specified range, cannot be duplicated, by randomly beating the number of normal ranges to achieve
{
if (min <= 0 | | Max < 0 | | min >= Max | | n < 0)
{
return NULL;
}

int *pnums = new Int[n];
if (NULL = = pnums)
{
return NULL;
}

Srand (seed);
Seed = rand ();

    int *temp = new Int[max+1];
    if (bnegative = = False)
    {
         for (int i = 0; I <= max; i++)
        {
    & nbsp;       Temp[i] = i;
       }
   }
    Else
    {
        for (int i = 0; i <= Max; i++)
        {
             Temp[i] =-I;
       }
   }

for (int i = max; i > min;i--)
{
int t = temp[i];
int s = rand ()% (i-min) + min;
Temp[i] = Temp[s];
Temp[s] = t;
}

for (int i = 0; i < n; i++)
{
Pnums[i] = temp[min++];
}
Delete temp;

return pnums;
}

int* RandomNoDumpNums3 (int min,int max,int n,bool bnegative)//Generate random numbers within a specified range, cannot be duplicated, use flag bits to implement

{
if (min <= 0 | | Max < 0 | | min >= Max | | n < 0)
{
return NULL;
}

int *pnums = new Int[n];
if (NULL = = pnums)
{
return NULL;
}

Srand (seed);
Seed = rand ();

int *temp = new Int[max+1];
if (bnegative = = False)
{
for (int i = 0; I <= max; i++)
{
Temp[i] =-1;
}
}
Else
{
for (int i = 0; I <= max; i++)
{
Temp[i] = 1;
}
}

int m = 0;
if (bnegative = = False)
{
for (int i = 0; i < n; i++)
{
m = rand ()% (max+1-min) + min;
while ( -1! = temp[m]);
Pnums[i] = m;
}
}
Else
{
for (int i = 0; i < n; i++)
{
m = rand ()% (max+1-min) + min;
while ( -1! = temp[m]);
Pnums[i] =-M;
}
}
Delete temp;

return pnums;
}

BOOL Istheredumplcates (int array[],int N)
{
Std::set<int> set_nums;
for (int i = 0; i < n; i++)
{
Set_nums.insert (Array[i]);
}
Return! (n = = Set_nums.size ());
}

Test procedure:

int _tmain (int argc, _tchar* argv[])
{
for (int i = 0; i <; i++)
{
int *p = Randomnums (1,15,15);
int *q = Randomnodumpnums (1,15,15);
int *q = RANDOMNODUMPNUMS2 (1,15,15);
int *q = RANDOMNODUMPNUMS2 (1,15,15);
if (NULL! = p)
//         {
for (int m = 0; m < 15;m++)
//             {
cout<<p[m]<< "-";
//             }
cout<<endl;
Delete p;
//         }
cout << "----------------------------------------" <<endl;
if (NULL! = q)
{
for (int n = 0; n < 15;n++)
{
cout<<q[n]<< "-";
}
cout<<endl;
if (Istheredumplcates (q,15))
{
cout<< "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" <<endl;
}
Delete q;
}
cout << "*****************************************" <<endl;
}

System ("pause");
return 0;
}

Using system functions to generate random numbers

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.