[ create a new paste ] login | about

Link: http://codepad.org/7kUIunTs    [ raw code | output | fork ]

C, pasted on Oct 1:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define HIST 10

double urandom(void)
{
	return rand() / (RAND_MAX + 1.0);
}

int main()
{
	int hist[HIST] = {0};
	int i, h;
	double d;

	srand((unsigned)time(NULL));
	for (i = 0; i < 100; i++) {
		d = urandom();
		printf("%f\n", d);
		h = d * HIST;
		if (h < 0 || HIST <= h) {
			fprintf(stderr, "error: h=%d d=%f\n", h, d);
			return 1;
		}
		hist[h]++;
	}
	for (i = 0; i < HIST; i++) {
		printf("%d=%d\n", i, hist[i]);
	}
	return 0;
}


Output:
0.157820
0.418911
0.207924
0.140227
0.248066
0.525510
0.793610
0.894935
0.345914
0.460501
0.314872
0.090011
0.673367
0.136445
0.836976
0.951610
0.868713
0.811040
0.581351
0.706454
0.350710
0.334568
0.668944
0.660228
0.510178
0.694496
0.119540
0.607717
0.202327
0.057078
0.103919
0.360147
0.475989
0.311842
0.500374
0.724054
0.837353
0.293984
0.618990
0.183267
0.754485
0.933861
0.273278
0.427852
0.070306
0.110254
0.379463
0.939019
0.921294
0.960814
0.645473
0.272004
0.295382
0.314416
0.932232
0.805559
0.008913
0.051772
0.413276
0.211240
0.108850
0.517195
0.571387
0.584839
0.829037
0.071761
0.308893
0.666390
0.365745
0.927883
0.849657
0.120230
0.861744
0.122935
0.548083
0.932050
0.233189
0.927545
0.871069
0.154483
0.888359
0.516541
0.426487
0.183740
0.830958
0.358719
0.989300
0.839870
0.410491
0.402576
0.051110
0.519341
0.919771
0.622497
0.104180
0.748808
0.694258
0.413073
0.415198
0.060003
0=8
1=13
2=9
3=11
4=10
5=10
6=10
7=5
8=13
9=11


Create a new paste based on this one


Comments: