
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
rand() and srand() in C
rand()
The function rand() is used to generate the pseudo random number. It returns an integer value and its range is from 0 to rand_max i.e 32767.
Here is the syntax of rand() in C language,
int rand(void);
Here is an example of rand() in C language,
Example
#include <stdio.h> #include<stdlib.h> int main() { printf("%d
", rand()); printf("%d", rand()); return 0; }
Output
1804289383 846930886
srand()
The function srand() is used to initialize the generated pseudo random number by rand() function. It does not return anything.
Here is the syntax of srand() in C language,
void srand(unsigned int number);
Here is an example of srand() in C language,
Example
#include <stdio.h> #include<stdlib.h> #include<time.h> int main() { srand(time(NULL)); printf("%d
", rand()); srand(12); printf("%d", rand()); return 0; }
Output
1432462941 1687063760
- Related Articles
- rand() and srand() in C/C++
- C++ Program to Use rand and srand Functions
- srand() function in PHP
- What is the use of randomize and srand functions in C language?
- rand() function in PHP
- PHP srand() Function
- PHP rand() Function
- ORDER BY rand() and keep them grouped in MySQL?
- How can I use RAND() function in an ORDER BY clause to shuffle MySQL set of rows?
- I am calling RAND() function two times in the same query then will it generate same random number two times or will it generate two different random numbers?
- Comma in C and C++
- Loops in C and C++
- Foreach in C++ and C#
- INT_MAX and INT_MIN in C/C++ and Applications
- isalpha() and isdigit() in C/C++

Advertisements