
- 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
What is the use of randomize and srand functions in C language?
If we are generating random numbers in a program, it is necessary to control the series of numbers.
The randomize() and srand() functions are used to seed the random number generator.
The process of assigning the random number generators starting number is called seeding the generators.
The randomize() uses PC’s clock to produce a random seed.
srand() allows us to specify the random number generator’s starting value.
Program
Given below is the C program on rand −
#include<stdio.h> int main(){ // create same sequence of // random numbers on every time the program runs for(int i = 0; i<10; i++) printf(" %d ", rand()); return 0; }
Output
You will see the following output −
1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421
Following is the C program on srand −
#include <stdio.h> #include <stdlib.h> #include<time.h> int main(){ // create different sequence of // random numbers on every time the program runs // It Use current time as seed for random generator srand(time(0)); for(int i = 0; i<10; i++) printf(" %d ", rand()); return 0; }
Output
You will see the following output −
1919778910 1203408690 1755813469 1976428341 37040990 1849384103 986990763 2040061815 391541163 1718314135
- Related Articles
- C++ Program to Use rand and srand Functions
- What is the use of sprintf() and sscanf() functions in C language?
- What are the predefined functions in C language?
- rand() and srand() in C
- rand() and srand() in C/C++
- What are string searching functions in C language?
- Explain the functions putw() and getw() in C language
- What is the use of Math.asinh() and Math.acosh() functions in javascript?
- Explain fgetc() and fputc() functions in C language
- Explain putc() and getc() functions of files in C language
- What are the high level I/O functions in C language?
- How communication among functions is established in C language?
- Explain unformatted input and output functions in C language
- What is the use of learning the Python language?
- State the difference between memcmp and memicmp functions in C language

Advertisements