/*devuelve un byte aleatorio a partir de varios bytes aleatorios de forma que aumenta la calidad del byte aleatorio devuelto*/
unsigned char Byte_Aleatorio(void);
/*comprueba el correcto funcionamiento del generador de numeros aleatorios*/
int Comprobar_Generador_Numeros_Aleatorios(void);
/*Genera un byte aleatorio mediante la recogida y tratamiento de bits aleatorios*/
unsigned char Byte_Aleatorio_Recogida(void);
/*comprueba si el buffer de bytes aleatorios pasa el test de monobit*/
int Test_monobit(unsigned char *buffer);
/*comprueba si el buffer de bytes aleatorios pasa el test de poker*/
int Test_poker(unsigned char *buffer);
/*comprueba si el buffer de bytes aleatorios pasa el test de runs*/
int Test_runs(unsigned char *buffer);
/*comprueba si el buffer de bytes aleatorios pasa el test de longrun*/
int Test_longrun(unsigned char *buffer);
/*pasa un buffer de bytes aleatorios por todos los tests disponibles*/
int Comprobar_Tests(void);
unsigned char Byte_Aleatorio_Recogida(void){
/*variables para la recogida de bits aleatorios*/
unsigned char c[32];
unsigned int u[32];
unsigned int b[32];
unsigned long e;
unsigned long *a;
unsigned int i;
/*variables de control*/
unsigned long o=0;
/*recogida de bits aleatorios y tratamiento rapido*/
i+= ((clock() % 101) + 1);
if(e != 0 && i != 0){
i+= e;
i++;
}
i+= ((clock() % 65000) + 3);
a= malloc(64 * sizeof(long));
if(a != NULL){
for(e=0; e < 64; e++){
if(*(a + e) != 0){
o++;
i+= (*(a + e));
}
}
i+= o;
free(a);
}
for(e=0; e < 32; e++){
if(u[e] != 0){
i+= u[e];
}
}
i+= ((clock() % 65000) + 3);
for(e=0; e < 32; e++){
if(b[e] != 0){
i+= b[e];
}
}
o= 0;
e= clock();
while(o <2>= 0){
o= (clock() - e);
}
i+= ((clock() % 32500) + 3);
for(e=0; e < 32; e++){
if(c[e] != 0){
i+= c[e];
}
}
/*tratamiento de los bits recogidos*/
i= i % 256;
return (unsigned char)i;
}
/*devuelve un byte aleatorio a partir de bytes aleatorios formados por la recogida de bits aleatorios, de forma que incrementa la calidad del byte aleatorio devuelto*/
unsigned char Byte_Aleatorio(void){
register unsigned char n, r, nr;
n= Byte_Aleatorio_Recogida();
r= Byte_Aleatorio_Recogida();
r= r % 13;
for(nr=0; nr < r; nr++){
n= Byte_Aleatorio_Recogida();
}
return n;
}
int Test_monobit(unsigned char *buffer){
register int unos=0,i,j;
register unsigned char ch;
for (i=0; i<2500; i++){
for (j=0; j<8>> (j)) & 1)) unos++;
}
}
if (9725<unos && unos<10275) return 0;
else return 1;
}
int Test_poker(unsigned char *buffer){
int f[16];
register int i;
register unsigned char ch;
register float x;
for (i=0; i<16; i++) f[i]=0;
for (i=0; i<2500>>4)&0xF)]++;
f[((ch)&0xF)]++;
}
x=0.0;
for (i=0; i<16; i++)
x += f[i]*f[i];
x = (x*16.0/5000.0)-5000.0;
if (2.16< x && x<46.17) return 0;
else return 1;
}
int Test_runs(unsigned char *buffer){
register int i,j,cuenta;
int r[6][2];
register unsigned char ch;
for(i=0; i<6>> (0)) & 1);
cuenta=0;
for (i=0; i<2500; i++) {
for (j=0; j<8>> (j)) & 1)==ch) cuenta++;
else {
if(cuenta>6){
cuenta=6;
}
r[cuenta-1][ch]++;
ch = !ch;
cuenta=1;
}
}
}
if(cuenta>6){
cuenta=6;
}
r[cuenta-1][ch]++;
for(i=0; i<2; i++){
if (r[0][i]<2343>2657) return 1;
if (r[1][i]<1135>1365) return 1;
if (r[2][i]<542> 708) return 1;
if (r[3][i]<251> 373) return 1;
if (r[4][i]<111> 201) return 1;
if (r[5][i]<111> 201) return 1;
}
return 0;
}
int Test_longrun(unsigned char *buffer){
register int i,j,maxc=-1,cuenta;
register unsigned char ch;
ch=(((*buffer) >> (0)) & 1);
cuenta=0;
for (i=0; i<2500; i++) {
for (j=0; j<8>> (j)) & 1)==ch) cuenta++;
else {
if (cuenta>maxc) maxc=cuenta;
ch=!ch;
cuenta=1;
}
}
}
if (cuenta>maxc) maxc=cuenta;
if (maxc>=26) return 1;
else return 0;
}
int Comprobar_Tests(void){
register int r=0, l;
unsigned char buffer[2500];
for(l=0; l<2500; l++){
buffer[l]= Byte_Aleatorio();
}
r+= Test_longrun(&buffer[0]);
r+= Test_runs(&buffer[0]);
r+= Test_poker(&buffer[0]);
r+= Test_monobit(&buffer[0]);
return r;
}
/*comprueba el correcto funcionamiento del generador de numeros aleatorios*/
int Comprobar_Generador_Numeros_Aleatorios(void){
register int r=0;
while(Comprobar_Tests() != 0 && r < 6){
r++;
}
if(r < 5){
r= 0;
}
return r;
}
srand(time(NULL));
rand()%101;
Con respecto a las versiones, no fui tan prolijo en ese sentido pero está todo hecho con clases en C++ y es bastante entendible el código.
carlostex escribió:Que tal, existen unas funciones en c que te permiten generar números aleatorios, sin que te preocupes por el algoritmo en si.
carlostex escribió:Sobre los números aleatorios, no hay manera posible que un sofware o formula matemática genere números aleatorios de verdad, para hacerlo se requiere de un hardware
Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 1 invitado