• Please review our updated Terms and Rules here

Randome program in C

jjzcp

Experienced Member
Joined
Sep 14, 2005
Messages
123
Location
on the bald a$$ prarie
Can someone write me a simple program in plain C (not c+, c++, etc.) that randomly picks a number from 1 to 10? Please make it as simple as possible. I have a couple C book's, and none of them decribes or even mentions the random function.
 
Last edited:
I believe this should do the trick:

Code:
#include <stdlib.h>
#define SEED 4711
 
int main(void) {
  int i;
 
  srand(SEED);
  i=1+(rand()%10);
  return i;
}

The srand() call seeds the random number generator, different seeds will give different random sequences. The rand() call will return an integer between 0 and the constant RAND_MAX. By using the modulo operator, we get the range 0..9, add with one to get 1..10.
 
I tried it under Turbo C 2.01, and i don't think it worked because Turbo C 2.01 was made in 1988. To see if it worked, how would i alter the program to make the random file appear on the screen?
 
Last edited:
Do you mean print out the random numbers?

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

int main(void) {
int i;

srand(time (NULL));
i = 1 + (rand () % 10);
printf ("i: <%d>\n", i);
return i;
}

Cheers,

80sFreak

P.S. I added the "time (NULL)" to make the numbers generated a bit more random.
 
The normal thing to do is to describe why it 'didn't work'. What was the compile error? More specifically, which library function call didn't it like?

What books are you using to learn C? I can recommend a few ... almost any book covers printf and library calls.
 
First off, i would like to thank carlsson, and 80's freak for the help.

80's freak, the random program you gave me worked excelent. One question though. I am a begginer in C, and my question is, what does time.h, and the srand(time (NULL)) do???

Mbbrutman, when i loaded the programs from the books, there was no error, the ccompiler just kept on repeating the same number, not a random number. The Book's i am using are '"C By Example", and "priemer c".
 
time.h is a header file for time related functions.

time(NULL) will read the current time as a time_t structure. The argument is normally used to store time into that variable, but using NULL it will only be returned.

srand() initializes the random number generator; in my case I used a fixed constand while 80's Freak used the current time as initializing seed to get different random numbers for every time you run the program. I suppose you in the end will write a more advanced program than getting one random number and then exit, so either way is good for you to know; sometimes having a fixed seed so you get the same random sequence everytime you run the program is better from a debugging point of view.
 
C is a little different than other languages. When we talk about a language like BASIC or Java, the 'runtime support' of the language is pretty well defined and it's the same across multiple platforms. (This is guaranteed in Java .. it's quite a bit looser for BASIC.) C guarantees a very minimal runtime.

So the question is, what is the runtime vs what is the language? The language defines how to string keywords together to make a program that compiles. The runtime tells you what functions are available in the libraries to call. Very often the library functions have to make another call to the operating system to get the work done. These are the functions that vary from platform to platform.

For example, printf is part of the standard C runtime provided on every platform. However, printf has to call to the OS (or even the BIOS of the machine) to actually get those characters on the screen. Same with the time() call and many others. And depending on the platform, some calls don't exist.

The examples from your books probably didn't work because the library calls that they used don't match what your runtime or OS are expecting. It's ok to use the examples in the book, but if the book wasn't written explicitly for your C compiler and runtime then you need to make sure that the library calls that it is using are being used correctly.
 
Back
Top