OT: algorithm for generating all possible combinations with replacement

Mike McCarty mike.mccarty at sbcglobal.net
Mon Nov 28 23:09:55 UTC 2005


Globe Trotter wrote:
> Dear all,
> 
> I have n objects and I want to select k of these with replacement. Do you know
> of code which would generate all the possible arrangements? Note that this is
> different from the selection of k of n objects without replacement and wanting
> to generate all the possible permutations.
> 
> Any suggestions? Existing C code would be fantastic btw, but I would be happy
> with an algorithm.
> 
> Best wishes!

Umm, sounds easy to me. Can you generate random uniform deviates
on [0,1) ? If so, then just do so. For example, suppose you want
to sample 15 objects 6 times. Then this works:

	#include <math.h>

	extern double Uniform(void);

	#define NUM_OBJECTS	15
	#define NUM_SAMPLES	 6

	int	Sample[NUM_SAMPLES];

	for (SampleNum = 0; SampleNum < NUM_SAMPLES; SampleNum++)
		Sample[SampleNum] = floor(0.5+Uniform()*NUM_OBJECTS);

Mike
-- 
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
You have found the bank of Larn.
I can explain it for you, but I can't understand it for you.
I speak only for myself, and I am unanimous in that!




More information about the fedora-list mailing list