Solved

# random permutations

Posted on 2003-03-01
Medium Priority
463 Views
Hi!

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.

Thanks a lot!

Sky
0
Question by:sky0707
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 8

Expert Comment

ID: 8048679
Try starting this yourself and then post here with any troubles you are having.

Exceter
0

LVL 8

Accepted Solution

akshayxx earned 375 total points
ID: 8054640
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
0

Author Comment

ID: 8061461
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! :)
0

LVL 8

Expert Comment

ID: 8061738
u r always welcome
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month12 days, 13 hours left to enroll