ftp.nice.ch/pub/next/science/mathematics/Random.2.0.N.bs.tar.gz#/Random2.0/Source/TestPercent.m

This is TestPercent.m in view mode; [Download] [Up]

//
// TestPercent.m
//
// This program tests the Random classes' abilities to generate
// random percentages.
//


#import "Random.h"
#import "StandardEngine.h"
#import "ElkinsEngine.h"
#import "R250Engine.h"
#import <math.h>
#import <stdio.h>
#import <stdlib.h>


#define DEFAULT_N	10000
#define DEFAULT_M	20


#define MAX_BAR		40


//
// test_percent()
//
// This function takes the id of a prepared Random
// instance and prints out the percent_test
// histogram using it.
//

int test_percent(id myRand, int m, int n)
{
    int		max;				// Size of largest bin.
    int		total;
    int		i;				// Generic loop variable.
    int		j;				// Generic loop variable.
    double	x;				// The random number.
    int		slot;				// The sorted position of a number.
    int		barsize;			// The length of a result bar.
    int		*slots;				// The bin array (dynamically allocated).
    
    //
    // Allocate and initialize slot array:
    //
    
//    printf("Initializing slot array...\n");
    
    slots = (int *)malloc(m * sizeof(int));
    for(i = 0; i < m; i++)
        slots[i] = 0;
    
    //
    // Make n random numbers:
    //
    
//    printf("Making %d random numbers...\n", n);
    
    max = 0;
    for(i = 0; i < n; i++) {
        x = [myRand percent];
	
	slot = (int)floor(x * m);
	if((slot >= 0) && (slot < m)) {		// If it is in range,
	    if((++slots[slot]) > max) {		//   count it.
		max = slots[slot];
	    }
	}
	else {
	    printf(">> Value %f out of range!\n", x);
	}
    }
    
    //
    // Print the results:
    //
    
//    printf("Printing the results...\n");
    
    total = 0;
    for(i = 0; i < m; i++) {
        printf("%1.6f - %1.6f: %6d:", i * (1.0 / m), (i + 1) * (1.0 / m), slots[i]);
	
	total += slots[i];
	barsize = (int)((double)slots[i] / ((double)max / (double)MAX_BAR));
	for(j = 0; j < barsize; j++)
	    printf("*");
	printf("\n");
    }
    
    printf("Max =   %6d\n", max);
    printf("Total = %6d\n", total);
    
    //
    // Clean up:
    //
    
    free(slots);
    
    //
    // Return to caller:
    //
    
    return 0;
}


//
// main()
//

int main(int argc, char *argv[])
{
    id		myRand;				// Random number generator.
    int		n;				// Quantity of numbers to generate.
    int		m;				// Number of slots in which to sort them.
    
    switch(argc) {
        case 2:
	    sscanf(argv[1], "%d", &n);;
	    m = DEFAULT_M;
	    break;
	case 3:
	    sscanf(argv[1], "%d", &n);;
	    sscanf(argv[2], "%d", &m);;
	    break;
	default:
	    n = DEFAULT_N;
	    m = DEFAULT_M;
	    break;
    }
    
    printf("TestPercent: Testing StandardEngine class:\n");
    myRand = [[Random alloc] initEngineClass:[StandardEngine class]];
    test_percent(myRand, m, n);
    printf("\n\n");
    [myRand free];
    
    printf("TestPercent: Testing ElkinsEngine class:\n");
    myRand = [[Random alloc] initEngineClass:[ElkinsEngine class]];
    test_percent(myRand, m, n);
    printf("\n\n");
    [myRand free];
    
    printf("TestPercent: Testing R250Engine class:\n");
    myRand = [[Random alloc] initEngineClass:[R250Engine class]];
    test_percent(myRand, m, n);
    printf("\n\n");
    [myRand free];
    
    return 0;
}


//
// End of file.
//

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.