Random numbers in C

Started by
6 comments, last by Zahlman 15 years, 4 months ago
hi, I've been trying to generate random numbers in C, i've looked at a few online examples, such as the one here: http://wakish.info/?p=19 However on the line where it says random = random(100); I get the compiler error: Parse Error, expecting `'}'' 'int' if someone could do a quick example of generating a simple number 1 to 10 i'd appriciate it, or point me in the right direction. thanks
Advertisement
The code example doesn't make any sense. Look at the man page for rand().
The variable name collides with the function name, change that.

That's how you do it.

#include <stdio.h>#include <time.h>#include <stdlib.h>int main(void){	srand((unsigned)time(0));	int random = rand() % 10; // 0 - 9	printf("random = %d", random);		getchar();	return 0;}
In C, you can use a combination of the functions rand(), srand(), time() and the constant RAND_MAX to do what you need.

These numbers are pseudo random, the same seed will produce the same sequence of numbers. So the first thing you will do is call srand(). To ensure the seed is different every time, use the time() function. Note: a single call to srand() somewhere near the start of the program is all you need. Don't call srand() repeatedly.

Then, a call to rand() will generate a number from 0 to RAND_MAX. There are two common ways to narrow down the range, the mod operator and some floating point arithmetic. While for most applications the mod operator works fine, it does give a greater bias to the lower bits of the random numbers, which may not have the same randomness as the full number.
#include <time.h>#include <stdlib.h>int random_int(int low, int high){    int range = high - low;    float r = rand() / (float)RAND_MAX;    return (int)(r * range) + low;}int main(){    srand(time(NULL));    while(1)    {       printf("Your random number is: %d\n", random_int(1,10));       getch();    }}

You can easily change random_int to random_float by replacing "int" with "float".
thanks everyone, I got it now
When you seed a random number using time, your number changes every second. Is there are different way to seed random from something that changes every time use it?
Quote:Original post by Heartless
When you seed a random number using time, your number changes every second. Is there are different way to seed random from something that changes every time use it?

srand() is only called once, before the first use of rand(). rand() will give you a new number every time you call it.
My Blog
Quote:Original post by Heartless
When you seed a random number using time, your number changes every second. Is there are different way to seed random from something that changes every time use it?


The point of seeding is to do it only once. That's why it's called seeding; you plant the seed, and the whole stream of random numbers grows from there. (If there *were* a way of giving a seed number each time, such that you got good random numbers out, you could just use the seed numbers directly as a random number stream.)

This topic is closed to new replies.

Advertisement