Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   random subset made by random selection of different elements from a given set (http://www.go4expert.com/forums/random-subset-random-selection-elements-t21358/)

random subset made by random selection of different elements from a given set

Hey all,

I am just a beginner in C/C++. I want to write a code in C/C++ the can select randomly a subset of integers both different from a given set. To be more concrete I have the following situation:

A=[1,2,3,4,5,6,7,8,9,10],

I want to make a subset of 4 elements from A whose element are chosen from A randomly and those element must be different from each other. I need a sets like :

B=[1,7,4,9] or C=[2,10,7,1] or D=[10,3,5,6] ....

Is there any one can help me?

Thank you so much

 xpi0t0s 15Mar2010 15:35

Re: random subset made by random selection of different elements from a given set

Pick 4 random numbers. Check if any are equal. If they are, change one. Repeat until you have no more duplicates.

Re: random subset made by random selection of different elements from a given set

Thank you for you idea,
the problem is how to do what u say in c/c++?

 xpi0t0s 15Mar2010 16:54

Re: random subset made by random selection of different elements from a given set

Do you know how to pick one random number?

 virxen 16Mar2010 02:37

Re: random subset made by random selection of different elements from a given set

for random numbers check here
http://www.java2s.com/Code/C/Math/Ge...wtouserand.htm

 All times are GMT +5.5. The time now is 05:56.