Randomly generated m numbers between 0 and N

Source: Internet
Author: User

Turn from: http://www.verydemo.com/demo_c161_i80057.html


Randomly generated m numbers between 0 and N

How to generate a non-repeat number of m between 0 and N with random numbers

1, the most direct way is to randomly generate a number between 0 to N, to determine whether this number has been selected, if not previously selected, then select, if previously selected, discard

void Common (int n,int m)
{
	int * randnum= (int *) malloc (n*sizeof (int));
	memset (randnum,0,n*sizeof (int));   Place all n positions at 0
	Srand (Time (NULL));
	while (m)
	{
		int cur=rand ()%n;
		if (randnum[cur]==0)   //Make a judgment, select and output
		{
			cout<<cur<<endl;
			if the current number is not selected Randnum[cur]=1;
			m--
		}
	}
	Free (randnum);

}

This approach is easy to understand, but requires extra space to ensure that the number of removed is not repeated, so we have a simpler way, the answer is yes

2, first on the code, after the explanation

void Mrand (int n, int m)
{

	Srand (Time (NULL));
	for (int i=0;i<n;i++)
	{	
		if (rand ()% (n-i) <m)
		{
			cout<<i<<endl;
			m--
		}}
		
	}

The above code is concise, but not easy to understand, we next explain

The first is a loop, which ensures that the number of outputs is not duplicated, because every time I am not the same

The second number is M, in which the rand ()% (n-i) <m is used to determine if the number is less than M, and M minus 1 if the condition is met until 0, which means the number of M has been taken.

Again is how to ensure that the number of M is equal to the probability of

In the first cycle of i=0, n-i=n, then the random number is generated by the 0-n-1 between the random number, then at the moment 0 is taken to the probability of m/n-1
I=1,n-i=n-1 in the second cycle, the random number generates a random number between 0-n-2, at which point the probability of 1 being taken is related to the 0 of the previous cycle. Let's say that in the last loop, not taken, then the probability of the 1 is m/n-2; assuming that the last cycle has been taken, then the probability of taking 1 is m-1/n-2, so the probability of this being taken in general is (1-m/n-1) * (m/n-2) + (m/n-1) * (m-1/ N-2), the result of the final Tong merging is the same as the probability of m/n-1 and the first time
Similarly, the probability of I being taken is also m/n-1 in the first cycle.

So the m number is equal to the probability.



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.