Numero aleatorios en C++
Hola a tdos necesito un programa que me muestre numero aleatoriamenmte, ya que utilizando RAND(), siempre me salen los mismos numeros.
Gracias.
Gracias.
Hola Diego:
Los números aleatorios permiten trabajar con la misma secuencia para poder detectar errores. Cuando el programa esté validado, se utiliza randomize() para generar secuencias nuevas cada vez.
gmantil
Los números aleatorios permiten trabajar con la misma secuencia para poder detectar errores. Cuando el programa esté validado, se utiliza randomize() para generar secuencias nuevas cada vez.
gmantil
Prueba con srand, en el MSDN puedes encontrar este ejemplo muy bueno par este caso.
/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6dn", rand() );
}
/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6dn", rand() );
}
