bordo

Genera un numero pseudocasuale compreso.Una versione più sicura di questa funzione è disponibile, vedere rand_s.

int rand( void );

Valore restituito

rand restituisce un numero pseudocasuale compreso, come descritto in precedenza.Non sono presenti ritorni di errore.

Note

rand la funzione restituisce un Integer pseudocasuale compreso nell'intervallo 0 e RAND_MAX (32767).utilizzare srand utilizzare per avviare il generatore di pseudoaccidentale-numero prima di chiamare rand.

Requisiti

routine

Intestazione di associazione

rand

<definito>

per informazioni di compatibilità aggiuntive, vedere compatibilità nell'introduzione.

Esempio

// crt_rand.c
// This program seeds the random-number generator
// with the time, then exercises the rand function.
//

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void SimpleRandDemo( int n )
{
   // Print n random numbers.
   int i;
   for( i = 0; i < n; i++ )
      printf( "  %6d\n", rand() );
}

void RangedRandDemo( int range_min, int range_max, int n )
{
   // Generate random numbers in the half-closed interval
   // [range_min, range_max). In other words,
   // range_min <= random number < range_max
   int i;
   for ( i = 0; i < n; i++ )
   {
      int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)
            + range_min;
      printf( "  %6d\n", u);
   }
}

int main( void )
{
   // Seed the random-number generator with the current time so that
   // the numbers will be different every time we run.
   srand( (unsigned)time( NULL ) );

   SimpleRandDemo( 10 );
   printf("\n");
   RangedRandDemo( -100, 100, 10 );
}
  

Equivalente .NET Framework

System:: classe casuale

Vedere anche

Riferimenti

Supporto per le operazioni in virgola mobile

srand

rand_s