Differenze tra le versioni di "Generare numeri casuali con le funzioni esterne srand() e rand() di stdlib.h"
Riga 8: | Riga 8: | ||
Mostriamo un esempio pratico: | Mostriamo un esempio pratico: | ||
Library "libc:6" | Library "libc:6" | ||
+ | |||
+ | <FONT Color=gray>' ''time_t time (time_t *__timer)'' | ||
+ | ' ''Return the current time and put it in *TIMER if TIMER is not NULL.''</font> | ||
+ | Private Extern time_C(__timer As Pointer) As Long Exec "time" | ||
<FONT Color=gray>' ''void srand (unsigned int __seed)'' | <FONT Color=gray>' ''void srand (unsigned int __seed)'' | ||
Riga 16: | Riga 20: | ||
' ''Return a random integer between 0 and RAND_MAX inclusive.''</font> | ' ''Return a random integer between 0 and RAND_MAX inclusive.''</font> | ||
Private Extern rand_C() As Integer Exec "rand" | Private Extern rand_C() As Integer Exec "rand" | ||
− | |||
− | |||
− | |||
− | |||
Riga 27: | Riga 27: | ||
<FONT Color=gray>' ''Inizializza il generatore di numeri casuali:''</font> | <FONT Color=gray>' ''Inizializza il generatore di numeri casuali:''</font> | ||
− | srand(time_C(0)) | + | srand(CInt(time_C(0))) |
− | <FONT Color=gray>' ''Stampa 10 numeri compresi fra ''' | + | <FONT Color=gray>' ''Stampa 10 numeri compresi fra '''1''' e '''100''':''</font> |
For b = 1 To 10 | For b = 1 To 10 | ||
− | Print rand_C() Mod 100 | + | Print rand_C() Mod 100 + 1 |
Next | Next | ||
Versione attuale delle 10:02, 7 mar 2018
Possiamo utilizzare le funzioni esterne srand( ) e rand( ), dichiarate nel file header "/usr/include/stdlib.h", per generare numeri casuali. [Nota 1]
La funzione esterna srand( ) in particolare inizializza il generatore di numeri casuali utilizzato successivamente dalla funzione esterna rand( ). Esso va inizializzato una volta soltanto e prima di usare la predetta funzione esterna rand( ). Ciò determina l'impostazione del seme (seed) per una sequenza di interi pseudo-casuali. La mancata impostazione del seme di partenza del generatore di numeri casuali mediante la funzione esterna srand( ) causa il ripetersi del medesimo risultato.
Va sottolineato che, avendo la funzione esterna rand( ) un nome identificatore identico alla funzione Rand( ) nativa di Gambas, nella sua dichiarazione con Extern si dovrà utilizzare un nome fittizio, e specificare poi con Exec il suo nome reale.
Mostriamo un esempio pratico:
Library "libc:6" ' time_t time (time_t *__timer) ' Return the current time and put it in *TIMER if TIMER is not NULL. Private Extern time_C(__timer As Pointer) As Long Exec "time" ' void srand (unsigned int __seed) ' Seed the random number generator with the given number. Private Extern srand(__seed As Integer) ' int rand (void) ' Return a random integer between 0 and RAND_MAX inclusive. Private Extern rand_C() As Integer Exec "rand" Public Sub Main() Dim b As Byte ' Inizializza il generatore di numeri casuali: srand(CInt(time_C(0))) ' Stampa 10 numeri compresi fra 1 e 100: For b = 1 To 10 Print rand_C() Mod 100 + 1 Next End
Note
[1] Vedi anche la pagina della Wiki: rand()