/*
** Random Demo
** -----------
** Demonstrates the randomness of functions FastRandom() and SlowRandom().
** Given the speed of FastRandom I think you will be quite impressed with
** it's results.  It gets all the numbers and gives a very even balance.
** The sequence of numbers is however not the best, so only use it when
** speed is critical.
**
** In case you are wondering the FastRandom() routine is just 10 assembler
** instructions including the re-seed and range div...  Wow!
**
** Compiles under SAS/C.
*/

#include <stdio.h>
#include <stdlib.h>
#include <proto/games.h>
#include <proto/exec.h>

struct GMSBase *GMSBase;
int YesNo;
ULONG loop, Iterations, UserRange;
UWORD *NumArray, Number;

/*=========================================================================*/
/*                               MAIN CODE                                 */
/*=========================================================================*/

void main(void)
{
   struct GamesLibrary *GMSBase = (struct GamesLibrary *)
       OpenLibrary("games.library", 0);
   if (GMSBase == NULL) exit(FALSE);

   printf("Random Number Demo");
   printf("\n------------------");
   printf("\nThis program demonstrates the randomness of the games.library functions");
   printf("\nFastRandom() and SlowRandom().  The FastRandom() is just 10 assembler");
   printf("\ninstructions and SlowRandom() is about twice that.  Both give very good");
   printf("\nresults, as you will see...\n\n");

   do {
        printf("Please enter a range between 1 and 50 : ");
        fflush(stdin);
        scanf("%d", &UserRange);
      }
   while (UserRange > 500 || UserRange < 1);

   printf("Enter the amount of iterations (Try 500000) : ");
   fflush(stdin);
   scanf("%d", &Iterations);

   printf("Do you want to view all %d generated numbers (Y/n)? ",Iterations);
   fflush(stdin);
   if (getchar() == 'n') YesNo = 1;

/*=========================================================================*/
/*                     GENERATE FAST RANDOM NUMBERS                        */
/*=========================================================================*/

   NumArray = calloc(UserRange,sizeof(UWORD));

   printf("\nPlease wait while generating %d FastRandom() numbers...\n",Iterations);

   loop = 0;
   do {
      Number = FastRandom(UserRange);
      if (YesNo == 0) printf("%d ", Number);
      NumArray[Number] += 1;
   }
   while (++loop < Iterations);

   printf("\nResults are:\n");

   for (loop=0; loop<UserRange; loop++) {
      printf("%4d: %d\n", loop, NumArray[loop]);
      NumArray[loop] = 0;
   }

/*=========================================================================*/
/*                     GENERATE SLOW RANDOM NUMBERS                        */
/*=========================================================================*/

   printf("\nPlease wait while generating %d SlowRandom() numbers...\n",Iterations);

   loop = 0;
   do {
      Number = SlowRandom(UserRange);
      if (YesNo == 0) printf("%d ",Number);
      NumArray[Number]++;
   }
   while (++loop < Iterations);

   printf("\nResults are:\n");

   for (loop=0; loop<UserRange; loop++) {
      printf("%4d: %d\n", loop, NumArray[loop]);
   }

   CloseLibrary((struct Library *)GMSBase);
}

/*=========================================================================*/

