Generator liczb pseudolosowych
#include <stdlib.h> /* standart library */ #include <stdio.h> /* standart input output */ #include <math.h> /* math's functions - like power */ void StartRandom(); double Random(); static double x1, x2, x3; int main () { unsigned long i; double t, x; printf("\n\n\n\n\n\n\n\n\n\n"); StartRandom();
/* first to check */ t = Random(); /* searching first repeat */ for(i=0 ; i < 1000000000; i++) { x = Random(); if (x == t) {printf("Powtorka : %ld ",i); exit(1);} if (!(i%1000000)) { printf("\b\b\b\b\b\b\b\b\b%.7ld ",i); } } getchar(); return 0; } /* ***************************************** */ /* gives random nuber from <0,1) range */ /* ***************************************** */
double Random () { double res = (x1 * 23.0) + (x2 * 268435451.0) + (x3 * 2333333.0); res = fmod(res,268435455.0); x1 = x2; x2 = x3; x3 = res; res /= 268435455.0; return res; }
/* ***************************************** */ /* random initializing */ /* ***************************************** */ void StartRandom () { int i; /* initialazing seed also can get some rand from time & date */ x1 = pow(2,3) - 1; x2 = pow(2,11) - 1; x3 = pow(2,17) - 1; /* to get stady numbers */ for(i = 0; i < 2000; i++) Random(); }
2012.11.22 22:29:08.