Skip to main content
added 310 characters in body
Source Link
ugoren
  • 17.6k
  • 5
  • 54
  • 75

C, 197 177178 161 chars

EDIT: Using a new random function, which is much shorter - reads a 4-digit integer s and uses s%64. Each 6-digit decimal number made of 0 and 1 only, taken %64 results in a unique result, so the randomness is good.
This approach consumes much more random bits, but is significantly shorter.

B[52],t,s,i=104; r(){forscanf(s=0"%6d",t=1;t<=i;t*=2)s|=getchar(&s)%2*t;s>i&&r;s%=64;s>i&&r();} main(){ for(;i--;r();)B[i%52]=i<52 ?r(),t=B[s],B[s]=B[i],printf("%c%c\n","23456789ATJQK"[t/4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n/4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1<<n is at least the desired maximum. If the result is more than the maximum - retry.

C, 197 177 chars

B[52],t,s,i=104; r(){for(s=0,t=1;t<=i;t*=2)s|=getchar()%2*t;s>i&&r();} main(){ for(;i--;r())B[i%52]=i<52 ?t=B[s],B[s]=B[i],printf("%c%c\n","23456789ATJQK"[t/4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n/4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1<<n is at least the desired maximum. If the result is more than the maximum - retry.

C, 197 178 161 chars

EDIT: Using a new random function, which is much shorter - reads a 4-digit integer s and uses s%64. Each 6-digit decimal number made of 0 and 1 only, taken %64 results in a unique result, so the randomness is good.
This approach consumes much more random bits, but is significantly shorter.

B[52],t,s,i=104; r(){scanf("%6d",&s);s%=64;s>i&&r();} main(){ for(;i--;)B[i%52]=i<52 ?r(),t=B[s],B[s]=B[i],printf("%c%c\n","23456789ATJQK"[t/4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n/4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1<<n is at least the desired maximum. If the result is more than the maximum - retry.

edited body
Source Link
ugoren
  • 17.6k
  • 5
  • 54
  • 75

C, 197 177 chars

B[52],t,s,i=104; r(){for(s=0,t=1;t<=i;t*=2)s|=getchar()%2*t;s>i&&r();} main(){ for(;r(i;i--;r();)B[i%52]=i<52 ?t=B[s],B[s]=B[i],printf("%c%c\n","23456789ATJQK"[t/4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n/4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1>>n1<<n is at least the desired maximum. If the result is more than the maximum - retry.

C, 197 177 chars

B[52],t,s,i=104; r(){for(s=0,t=1;t<=i;t*=2)s|=getchar()%2*t;s>i&&r();} main(){ for(;r(i--);)B[i%52]=i<52 ?t=B[s],B[s]=B[i],printf("%c%c\n","23456789ATJQK"[t/4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n/4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1>>n is at least the desired maximum. If the result is more than the maximum - retry.

C, 197 177 chars

B[52],t,s,i=104; r(){for(s=0,t=1;t<=i;t*=2)s|=getchar()%2*t;s>i&&r();} main(){ for(;i--;r())B[i%52]=i<52 ?t=B[s],B[s]=B[i],printf("%c%c\n","23456789ATJQK"[t/4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n/4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1<<n is at least the desired maximum. If the result is more than the maximum - retry.

added 1 characters in body
Source Link
ugoren
  • 17.6k
  • 5
  • 54
  • 75

C, 197197 177 chars

d[52]B[52],t,s,i;i=104; r(){for(t=0s=0,s=1;s<i+2;s*=2t=1;t<=i;t*=2)t|=getchars|=getchar()%2*s;return t>i?r%2*t;s>i&&r():t;;} main(){ for(i=104;i;r(i--);)d[i%52]=i<52B[i%52]=i<52 ?t=d[s=r()],d[s]=d[i]t=B[s],s=t%13B[s]=B[i],printf("%c%c\n",s>7?"ATJQK"[s-8]:s+50,"cdhs"[t"23456789ATJQK"[t/13]4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n%13=rank, nn/14=suit4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1>>n is at least the desired maximum. If the result is more than the maximum - retry.

C, 197 chars

d[52],t,s,i; r(){for(t=0,s=1;s<i+2;s*=2)t|=getchar()%2*s;return t>i?r():t;} main(){ for(i=104;i--;)d[i%52]=i<52 ?t=d[s=r()],d[s]=d[i],s=t%13,printf("%c%c\n",s>7?"ATJQK"[s-8]:s+50,"cdhs"[t/13]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n%13=rank, n/14=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1>>n is at least the desired maximum. If the result is more than the maximum - retry.

C, 197 177 chars

B[52],t,s,i=104; r(){for(s=0,t=1;t<=i;t*=2)s|=getchar()%2*t;s>i&&r();} main(){ for(;r(i--);)B[i%52]=i<52 ?t=B[s],B[s]=B[i],printf("%c%c\n","23456789ATJQK"[t/4],"cdhs"[t%4]),t :i-52; } 

The basic logic is simple - initialize an array of 52 ints with 0..51, shuffle (randomally replace element x with another from the range 0..x), print formatted (n/4=rank, n%4=suit).
One loop, that runs 104 times, does initialization (first 52 runs), shuffling and printing (last 52 runs).
A random number is generated by pulling n random bits, until 1>>n is at least the desired maximum. If the result is more than the maximum - retry.

Source Link
ugoren
  • 17.6k
  • 5
  • 54
  • 75
Loading