473,471 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Randomly permute a list of integers

Hi,

Please excuse me if this is not the right forum for this question. I
would like to create a random permutation of a list of numbers. How can
I do this in C?

I was just going to draw a number from 1 to n. n is the number of items
in the list and place the items in a new list in the order that their
index is drawn from the random number generator. Is there a library
function to do this?

Regards,

Michael

Aug 8 '06 #1
6 7957
Michael McGarry wrote:
Hi,

Please excuse me if this is not the right forum for this question. I
would like to create a random permutation of a list of numbers. How can
I do this in C?

I was just going to draw a number from 1 to n. n is the number of items
in the list and place the items in a new list in the order that their
index is drawn from the random number generator. Is there a library
function to do this?
This is Question 13.19 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://c-faq.com/

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 8 '06 #2
"Michael McGarry" <mi*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Hi,

Please excuse me if this is not the right forum for this question. I
would like to create a random permutation of a list of numbers. How can
I do this in C?

I was just going to draw a number from 1 to n. n is the number of items
in the list and place the items in a new list in the order that their
index is drawn from the random number generator. Is there a library
function to do this?
It is trivial to make it. There are quality bugs in the code below.

#include <stdlib.h>

void shuffle(int *arr, int arr_size)
{
int tmp;
size_t i;
for (i = 0; i < arr_size; i++) {
int rval = rand() % arr_size;
tmp = arr[i];
arr[i] = arr[rval];
arr[rval] = tmp;
}
}

#ifdef UNIT_TEST

#include <stdio.h>

typedef enum suite {
club=1, heart=2, diamond=4, spade=8
} suite;

typedef enum face {
deuce = 16, trey=32, four=64, five=128, six=256, seven=512,
eight=1024,
nine=2048, ten=4096, jack=8192, queen=16384, king=32768, ace=65536
} face;

int dequeue[52];

void decipher_face(int input)
{
switch ((input >4) << 4) {
case deuce:
printf("deuce ");
break;
case trey:
printf("trey ");
break;
case four:
printf("four ");
break;
case five:
printf("five ");
break;
case six:
printf("six ");
break;
case seven:
printf("seven ");
break;
case eight:
printf("eight ");
break;
case nine:
printf("nine ");
break;
case ten:
printf("ten ");
break;
case jack:
printf("jack ");
break;
case queen:
printf("queen ");
break;
case king:
printf("king ");
break;
case ace:
printf("ace ");
break;
default:
printf("unknown ");
break;
}
}

void decipher_suite(int input)
{
switch (input & 0xF) {
case club:
puts("of clubs.");
break;
case heart:
puts("of hearts.");
break;
case diamond:
puts("of diamonds.");
break;
case spade:
puts("of spades.");
break;
default:
puts("unknown ");
break;
}
}

int main(void)
{
suite s_index;
face f_index;
size_t index;
int where = 0;
/* Make the list... */
puts("\nMake the list...");
for (s_index = club; s_index <= spade; s_index<<=1)
for (f_index = deuce; f_index <= ace; f_index<<=1) {
dequeue[where++] = (size_t) s_index + (size_t) f_index;
}

/* Dump the original list */
puts("\nDump the original list...");
for (index = 0; index < sizeof dequeue / sizeof dequeue[0];
index++)
{
decipher_face(dequeue[index]);
decipher_suite(dequeue[index]);
}
/* Shuffle the list... */
puts("\nShuffle the list...");
shuffle(dequeue, sizeof dequeue / sizeof dequeue[0]);

/* Dump the shuffled list */
puts("\nDump the shuffled list...");
for (index = 0; index < sizeof dequeue / sizeof dequeue[0];
index++)
{
decipher_face(dequeue[index]);
decipher_suite(dequeue[index]);
}
return 0;
}
#endif

Aug 8 '06 #3
dc*****@connx.com wrote:
"Michael McGarry" <mi*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>>Hi,

Please excuse me if this is not the right forum for this question. I
would like to create a random permutation of a list of numbers. How can
I do this in C?

I was just going to draw a number from 1 to n. n is the number of items
in the list and place the items in a new list in the order that their
index is drawn from the random number generator. Is there a library
function to do this?


It is trivial to make it. There are quality bugs in the code below.
[...]
"Quality bugs" -- How do they differ from plain old "bugs?"

See the FAQ; the solution given there is just as short, just
as simple, and doesn't introduce bias (beyond whatever is provided
by the underlying random number generator, that is).

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 8 '06 #4
Cool. I was not expecting there to have been something in the FAQ. I am
pleasantly surprised.

Thank you very much,

Michael

Eric Sosman wrote:
Michael McGarry wrote:
Hi,

Please excuse me if this is not the right forum for this question. I
would like to create a random permutation of a list of numbers. How can
I do this in C?

I was just going to draw a number from 1 to n. n is the number of items
in the list and place the items in a new list in the order that their
index is drawn from the random number generator. Is there a library
function to do this?

This is Question 13.19 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://c-faq.com/

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 8 '06 #5
Michael McGarry wrote:
Cool. I was not expecting there to have been something in the FAQ. I am
pleasantly surprised.
Ah, Grasshopper, much to learn you have yet. Yours to
command is the deep wisdom of the FAQ -- but only if you can
first wisdom what it is to command imagine. (Snort.) And
only if you can first read. (Snicker.)

"A day without the FAQ is like a day without getting your
tonsils ripped out by Dan Pop."

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 9 '06 #6

Eric Sosman wrote:
dc*****@connx.com wrote:
"Michael McGarry" <mi*************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>Hi,

Please excuse me if this is not the right forum for this question. I
would like to create a random permutation of a list of numbers. How can
I do this in C?

I was just going to draw a number from 1 to n. n is the number of items
in the list and place the items in a new list in the order that their
index is drawn from the random number generator. Is there a library
function to do this?

It is trivial to make it. There are quality bugs in the code below.
[...]

"Quality bugs" -- How do they differ from plain old "bugs?"
It is introduced/left in as an exercise for the reader.
See the FAQ; the solution given there is just as short, just
as simple, and doesn't introduce bias (beyond whatever is provided
by the underlying random number generator, that is).
That fixes one of them.
--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h>...
16
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
2
by: Yuan Zhong | last post by:
hello all, not sure if it's appropriate to post here. If not, I am sorry and please direct me to the right groups. Thanks. I write this function to permute a string. e.g. if the input string is...
7
by: Girish Sahani | last post by:
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1 == ti2 and not index1 !=...
9
by: Alan Isaac | last post by:
I need access to 2*n random choices for two types subject to a constraint that in the end I have drawn n of each. I first tried:: def random_types(n,typelist=): types = typelist*n...
11
by: Steve | last post by:
I'm trying to create a list range of floats and running into problems. I've been trying something like: a = 0.0 b = 10.0 flts = range(a, b) fltlst.append(flts)
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.