Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

random permutations


I need to write a program that reads in an Array of N numbers -say N is 10- and outputs 5 random permutations of these numbers (so for each permutation/shuffling the numbers have to be re-arranged in some random order).

I need to use an algorithm called shuffle(array, size) (INPUT: array of integers array, integer size. OUTPUT: reshuffled array) and an algorithm RandomNumber(limit) (INPUT integer limit and OUTPUT a random integer between 0 and limit (exclusive)).

I know I need to use the rand()function to implement randNumber(int). I also know I need to use srand and the time library.

Can  anyone please help me getting this started?

Thanks a lot!

  • 2
1 Solution
Try starting this yourself and then post here with any troubles you are having.

no full source codes are allowed for home work problems ..
but here is some hint that will get u started..

since you are concerned with only 5 random permutations of the given set of 10 numbers..
then to generate a single permutation of ur 'set'  u just need single pair of numbers to be swapped..
so to generate each of the five permutation.. here is how u go..
lets say ur main array is
int arr[10];
generate 5 unique random numbers in the range 0--4(including 0 and 4),
U might have to generate more than five random numbers to ensure that all 5 are unique

then for each random number 'r' generated .. swap arr[r] with arr[9-r]
i hope we need not tell u the algorithm to swap the two numbers..

so with single swapping u can have a permutation of the main array..

u can  generate more random numbers  to do more swappings .. but it will not be easy to keep the track that each of the five permutations are different from other.

with my method u'll have the permutation very less disturbed as compared to the main array .. but it ensures that all 5 permutations are different from each other and from main array
sky0707Author Commented:
Thanks for your comment, I didnt really use it because I finished my program before I got your comment, but I appreciate the fact that u tried to help me, thanks! :)
u r always welcome

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now