Skip to content

Instantly share code, notes, and snippets.

@moehuster
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moehuster/f5deb6dca94c4d3f11a6 to your computer and use it in GitHub Desktop.
Save moehuster/f5deb6dca94c4d3f11a6 to your computer and use it in GitHub Desktop.
#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