Re: [C][OT] erroneous fprintf output

From: Curley Q. (curleyq_at_bogus.net)
Date: 05/14/04


Date: Thu, 13 May 2004 22:07:34 GMT

Francis Glassborow wrote:

> My criticism is that awful selection mechanism that isn't even
> guaranteed to ever terminate when creating a single shuffle of the 100
> numbers. If you want to do it by random selection use a standard shuffle
> function:
>
> void swap_random_with_last(int array[], int size){
> int choice = rand() % size;
> int temp = array[choice];
> array[choice] = array[size-1];
> array[size-1] = temp;
> }
>
> void shuffle(int array[], int size){
> int i;
> for(i = size; i != 1; i--){
> swap_random_with last(array, i);
> }
>
> Now create an array of 100 entries and initialise them with 1 to 100 and
> call shuffle.
>
> However are you required to have an equal number of items in each (i.e.
> 50 : 50)?

No

> Consider creating two queues (stacks or whatever other image you like)
> and add the next square-root to whichever stack currently has the
> smaller total. Initially the values will go on alternating piles, but
> eventually the one that is getting the even numbers will get far enough
> ahead so that the square roots of a consecutive pair will both get added
> to the first pile, then they will alternate again. Actually
> investigating the pattern of when successive square roots go onto the
> same pile could be quite interesting, but that is another problem.
>
> If you are dealing with 100 numbers you just might find that this
> algorithm results in a pile of 51 and another of 49 but no worse than
> that (proving that is not very hard).

Have I correctly implemented your suggestions in the code below?

/* calculate the sqrts of 1-100 and divide
    them into 2 sets that are approx equal */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 100
void swap_random_with_last(int array[], int size);
void shuffle(int array[], int size);

int main(void)
{
   double sqroots[SIZE];
   int integers[SIZE];
   double set1, set2, total;
   int idx;

   set1 = set2 = total = 0.0;

   for(idx = 0; idx < SIZE; idx++)
     integers[idx] = idx + 1;

   srand((unsigned int)time(0));

   shuffle(integers, SIZE);

   for(idx = 0; idx < SIZE; ++idx)
   {
     sqroots[idx] = sqrt((double)integers[idx]);
     total += sqroots[idx];
   }

   for(idx = 0; idx < SIZE; ++idx)
     if(set1 == set2 || set1 < set2) /* equal on 1st pass */
       set1 += sqroots[idx];
     else
       set2 += sqroots[idx];

   printf("set1 = %f\n", set1);
   printf("set2 = %f\n", set2);
   printf("total = %f\n", total);

   return 0;
}

void swap_random_with_last(int array[], int size){
    int choice = rand() % size;
    int temp = array[choice];
    array[choice] = array[size-1];
    array[size-1] = temp;
}

void shuffle(int array[], int size){
    int i;
    for(i = size; i != 1; i--)
      swap_random_with_last(array, i);
}

> When you have two piles you may need to fine tune to get them more equal

To say the least.



Relevant Pages

  • Re: Better use of random number genator bits?
    ... This on average is a total of about 2880 bits to shuffle a deck (of course ... Let's choose a random 32-bit unsigned integer and check if it is ... int i, j, n; ... randnum_UL = KISS; ...
    (sci.math)
  • shuffle loses/duplicates objects in LinkedList
    ... the shuffle() the LinkedList is totally mangled, ... I'm using the GCC java compiler - might be interesting to see what happens ... // Simple Class which just contains an int. ... public class Int implements Comparable ...
    (comp.lang.java.help)
  • Re: Simple Casting Question
    ... my datasets are large enough that storing it as float or int ... only in the form (int to float),,, ... all the types that may store square roots, ... void f ...
    (comp.lang.c)
  • Re: Binary Shuffling
    ... A shuffle will make all the permutations of all the ... int genshuffle(char out) ... where makerandgenerates a random integer from 0 to n-1; ... The shuffle is a random style. ...
    (sci.physics)
  • Re: Binary Shuffling
    ... In sci.physics, Douglas Eagleson ... A shuffle will make all the permutations of all the ... duplicate 1 or 0 is a combination word, not a permutation word. ... int genshuffle(char out) ...
    (sci.physics)