[Lugge] R: Re: R: generare numeri casuali in c usando /dev/random o /dev/urandom

  • From: "gianlu]{a" <theblackangel@xxxxxxxx>
  • To: <lugge@xxxxxxxxxxxxx>
  • Date: Thu, 3 Feb 2005 11:18:01 +0100

 
Dunque come linea di principio il tuo ragionamento non è male, bisogna
vedere quello che devi farci e cosa vuol dire per te *random*.
I due pseudo file /dev/urandom e /dev/random servono per [citando IBM]

<<The /dev/random and /dev/urandom character devices provide
cryptographically secure random output generated from interrupt timings or
input written to the devices.

The /dev/random device is intended to provide high quality,
cryptographically secure random output and will only return output for which
sufficient (an equal or greater amount) random input is available to
generate the output. If insufficient random input is available, reads from
the /dev/random device will block until the request can be fulfilled unless
the O_NONBLOCK flag was specified when the device was opened, in which case
as much high quality output as could be generated is returned with the error
code EAGAIN.

The /dev/urandom device provides a reliable source of random output, however
the output will not be generated from an equal amount of random input if
insufficient input is available. Reads from the /dev/urandom device always
return the quantity of output requested without blocking. If insufficient
random input is available, alternate input will be processed by the random
number generator to provide cryptographically secure output, the strength of
which will reflect the strength of the algorithms used by the random number
generator. Output generated without random input is theoretically less
secure than output generated from random input, so /dev/random should be
used for applications for which a high level of confidence in the security
of the output is required. >>

Mentre srand e rand

<<The rand() function returns a pseudo-random integer between 0 and
RAND_MAX.

The srand() function sets its argument as the seed for a new sequence of
pseudo-random integers to be returned by rand(). These sequences are
repeatable by calling srand() with the same seed value.

If no seed value is provided, the rand() function is automatically seeded
with a value of 1. >>

Quindi la tua idea è buona, occhio però a quello che viene detto ovvero che
rand ritorna una sequenza di numeri pseudo-random e che a parità di seme
passato a srand è ripetibile!

Una sequenza pseudo random ripetibile la puoi ottenere anche così

/* Un semplice PseudoRandomNumberGenerator */
/* usando un algoritmo LCGs                      */     
/* hjan - theblackangel@xxxxxxxx           */

#include <stdio.h>;

#define max 70          /*numeri generati */
#define seed 1                  /* seed per il generatore di numeri */

FILE *output;                   /*File uscita numeri*/

main ()
{
int i, old, newx, newy;
 
old=seed;                               /*il seed*/

output = fopen("random.txt", "w");

/* generating random numbers */

for (i=0; i<=max; i=i++)
{
        /* Generatore di 16 numeri random nel campo 0-15 */
        newx = (5*old+3) % 16;
        fprintf (output, "%i\n", newx); 
        old=newx; /* cambio seed */
        
}

fclose (output);
}  

Okkio però, dai uno sguardo al file in uscita e capirai cosa vuol dire
sequenza ripetibile!
Ovviamente questo generatore si può migliorare con qualche accorgimento,
vedi seme iniziale diverso da 1 ecc.

Se hai domande son qui.

G

-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.4 - Release Date: 01/02/2005
 



--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.4 - Release Date: 01/02/2005



 --
 Email.it, the professional e-mail, gratis per te: http://www.email.it/f

 Sponsor:
 Stanco del tappetino mouse che hai??
* Stampaci l'immagine che vuoi tu!
*
 Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid!17&d=3-2
========----------

 Archivio delle e-mail postate in lista
 http://www.freelists.org/archives/lugge/

 Prima di scrivere in m-list per favore leggi il regolamento
 http://www.lugge.net/index.php?mod=cosa_facciamo/gruppo_di_discussione

 Modifica dell'account sulla lista LUGGe
 http://www.lugge.net/index.php?mod=cosa_facciamo/gruppo_di_discussione#list



Other related posts: