Last active
August 29, 2015 14:01
-
-
Save moehuster/f5deb6dca94c4d3f11a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
unsigned int get_rand(int x) | |
{ | |
unsigned int i,res,mask=0; | |
if (x<=0) return 0; | |
for (i=0; x>=(1<<i); i++) | |
mask=(mask<<1)+1; | |
while ((res=rand()&mask)>x); | |
return res; | |
} | |
void swap(int* a, int* b) | |
{ | |
int t=*a; *a=*b; *b=t; | |
} | |
void random_shuffle(int arr[], int n) | |
{ | |
int i; | |
for (i=n-1; i>0; i--) | |
swap(arr+i, arr+get_rand(n-1)); | |
} | |
int main() | |
{ | |
int i, cards[13]={1,2,3,4,5,6,7,8,9,10,11,12,13}; | |
srand((unsigned int)time(0)); | |
random_shuffle(cards, 13); | |
for (i=0; i<13; i++) | |
printf("%d ",cards[i]); | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment