PS-Trainer C - Entwicklung
Mathematik: Zufallszahlen
Homepage von PS-Trainer - C-Entwicklung - Mathematik - an PS-Trainer
PS-Trainer PS-Trainer

Math and related Random Numbers
rand Generate a pseudorandom number.
srand Set a random starting point.


rand
Generates a pseudorandom number.
int rand( void );

Routine Required Header Compatibility
rand <stdlib.h> ANSI, Win 95, Win NT

Remarks
The rand function returns a pseudorandom integer in the range 0 to RAND_MAX.
Use the srand function to seed the pseudorandom-number generator before calling rand.

Subject: Floating-Point Support Routines
Keywords: See also srand
Return Value
rand returns a pseudorandom number, as described above. There is no error return.

Example
/* 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,ir;
float rmax,fr;

/* Get pseudo-random range */
rmax = float(RAND_MAX);

/* Se
ed 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++ )
 ir = rand();
 fr = float(ir)/rmax;
 printf( " %6d %6f\n", ir,fr);
}


Output
 6929 0.001251
 8026 0.563585
21987 0.193304
30734 0.808740
20587 0.585009
 6699 0.479873
22034 0.350291
25051 0.895962
 7988 0.822840
10104 0.746605


srand
Sets a random starting point.
void srand( unsigned int seed );

Routine Required Header Compatibility
srand <stdlib.h> ANSI, Win 95, Win NT


Remarks
The srand function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.

Subject: Floating-Point Support Routine
Keywords: See also rand

Return Value
None

Parameter
seed Seed for random-number generation

Example
/* 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,ir;
float rmax,fr;


/* Get pseudo-random range */
rmax = float(RAND_MAX);


/* 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++ )
 ir = rand();
 fr = float(ir)/rmax;
 printf( " %6d %6f\n", ir,fr);
}


Output
 6929 0.001251
 8026 0.563585
21987 0.193304
30734 0.808740
20587 0.585009
 6699 0.479873
22034 0.350291
25051 0.895962
 7988 0.822840
10104 0.746605


Homepage von PS-Trainer - C-Entwicklung - Mathematik - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1