Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Sample with...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 4 Topic 45774 of 47034
Post > Topic >>

Re: Sample without replacement+intersect

by Jerry Coffin <jcoffin@[EMAIL PROTECTED] > May 6, 2008 at 11:25 PM

In article <feeb9a47-303c-46c4-a250-553fd735310b@[EMAIL PROTECTED]
>, franco@[EMAIL PROTECTED]
 says...
> Hi, I'm trying to sample without replacement some numbers (xons:70
> values out of 1 to 200 and xEPS1cover:150 values out of 1 to 200).
> Then I'm trying to intersect both samples to see how many are in
> common. I have tried to write the code below, but I cannot seem to
> sample without replacement (there are recurrent numbers within the
> same sample, I don't want that) and I cannot either find the exact
> intersection between the two samples. Can anyone please help or give
> some hints. Thanks

I'm afraid I'm a bit too lazy to figure out your code (Google appears to 
have lost the indentation).

One easy (if less than perfectly efficient) way to get samples without 
replacements is to generate the numbers in the range you want 
(consecutively), then select the first N items after scrambling them 
into a random order:

template <class T, class OutIt> 
void rand_select(T const &lower, T const &upper, size_t N, OutIt &it) { 
	std::vector<T> temp;

	for (T i=lower; i!=upper; ++i)
		temp.push_back(i);
	std::random_shuffle(temp.begin(), temp.end());
	std::copy(temp.begin(), temp.begin()+N, it);
}

Used something like:

	std::vector<int> items;

	rand_select(1, 200, 70, std::back_inserter(items));
	std::copy(items.begin(), items.end(), 
		std::ostream_iterator<int>(std::cout, "\t"));

For the numbers you've given above, this method is probably perfectly 
adequate. If the numbers might vary, especially so you're selecting only 
a tiny part of a huge range, this can be quite inefficient, and other 
ways will work better. In the latter case (if you're SURE the number 
being selected is small compared to the range, you're better off with 
something like selecting random numbers in the range, and inserting them 
into a set until you get as many numbers as you want:

template <class T, class OutIt>
void rand_select(T const &lower, T const &upper, size_t N, OutIt &it) { 
	std::set<T> selection;

	for (size_t i=0; i<N; ++i)
		selection.insert(rand_lim(lower, upper);
	while (selection.size() < N)
		selection.insert(rand_lim(lower, upper);
}

Where rand_lim is defined something like this:

int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
 */

	int divisor = RAND_MAX/(limit+1);
	int retval;

	do { 
		retval = rand() / divisor;
	} while (retval > limit);

	return retval;
}

int rand_lim(int lower, int upper) {
	int range = abs(upper-lower);

	return rand_lim(range) + lower;
}

You might prefer to use the uniform_int from TR1, which (I believe) has 
the same general intent as this, though it has a substantially different 
interface.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
 




 4 Posts in Topic:
Sample without replacement+intersect
Francogrex <franco@[EM  2008-05-06 06:23:20 
Re: Sample without replacement+intersect
Christopher <cpisz@[EM  2008-05-06 12:53:30 
Re: Sample without replacement+intersect
Jerry Coffin <jcoffin@  2008-05-06 23:25:36 
Re: Sample without replacement+intersect
Lionel B <me@[EMAIL PR  2008-05-07 08:58:57 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:59:38 CDT 2008.