TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: KURT KUZBA
from: BRUCE WEDDING
date: 1998-05-25 14:04:00
subject: ReadRandom

KK>   I know it is unreasonable to do so, but I
KK>   dislike the idea of accepting or rejecting a line based on
KK>   a 1/0 random, because it seems to me rather unlikely that
KK>   the latter members of the lines file would be neglected,
KK>   even more than poor line one in the given example.
Well, test it yourself.  I did and found a very good distribution.  You
have to learn to trust your tools, Kurt.

Line 0
  to
Line 9
#include 
#include 
#include 
#include 
#define STR_Z 256
#define FALSE  0
#define TRUE   !FALSE
// Returns a number between 1 and i
#define RAND_RANGE(i) (1 + (int)((double)rand() / ((double)RAND_MAX + 1) * 
)
int main(int c, char **v)
{
   char CurStrs[2][STR_Z];
   char KeepStr[STR_Z];
   int j, k, line[10] = {0};
   int i = 1, odds, choice;
   FILE *fp = fopen("c:\\test.lst", "rb");
   if (!fp)
     exit(EXIT_FAILURE);
   // Seed me Femour
   srand((unsigned) time(NULL));
   for ( j = 0; j < 10000; j++)
   {
     // Get the first String
     fgets(CurStrs[0], STR_Z, fp);
     // Read in the rest to EOF
     while( fgets(CurStrs[1], STR_Z, fp))
     {
       // Get a number in range with the
       // the number of strings read
       odds = RAND_RANGE(++i);
       // Select one of the strings, weighting
       // choice appropriately to previously read
       // string
       choice = ((odds == i) ? 1: 0);
       // Save the selected string
       strncpy(KeepStr, CurStrs[choice], STR_Z);
       // If we picked the newly read string
       // copy it into other array
       if ( choice == 1)
         strncpy(CurStrs[0], CurStrs[1], STR_Z);
     }
     for ( k = 0; k < 10; k++)
     {
       char buf[256];
       sprintf(buf, "%d", k);
       if (strstr(KeepStr, buf))
         line[k]++;
     }
     rewind(fp);
     i = 1;
   }
   for ( i = 0; i < 10; i++)
     printf("Line %d.  %d Occurences\n", i, line[i]);
   return 0;
}
Bruce
___
 X OLX 2.1 TD X Press any key to continue or any other key to quit
--- Maximus/2 3.01
---------------
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.