• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Pointer » Random Number Generation function
Next →
← Prev

Random Number Generation function

By Dinesh Thakur

Random number generation is a very useful facility in many programming situations, particularly gaming and simulation. We can also use it to generate test data for programs, particularly when a large amount of data is required. This saves a lot of time on data entry.

A random number is one whose value cannot be predicted. It is nearly impossible to get truly random numbers. However, one may use pseudorandom numbers which are generated. The C standard library provides a function named rand to generate pseudo-random numbers. This function is declared in the stdlib.h header file. It does not require any argument and returns an integer number in the range 0 to RAND_MAX, where RAND_MAX is a constant defined in stdlib.h with value 32767.

We can manipulate the value returned by the the rand function to generate integer or floating-point numbers in a desired range. The expression

(float) rand() / RAND_MAX

gives a random number of type float in the range [0.0, 1.0], i. e., 0 to 1.0, both inclusive. Note the use of typecast (float) to avoid integer division. To generate a random number in a desired range m ton, we can now use the following expression:

m + (m – n) * (float) rand() /RAND_MAX

The rand function actually generates pseudo-random data. Thus, each time the program is executed, it generates the same sequence of random numbers. This may not be acceptable in many situations. Hence, another function called srand is provided to reseed the random number generator. A typical call to this function takes the following form:

srand (seed)

where seed is an unsigned integer. This call reseeds the random number generator so that a different sequence of random numbers is generated on subsequent calls to rand.

Random numbers generation with rand() with default seed.

#include<stdio.h>
#include<stdlib.h>
int main ()
{
  int n ;
  clrscr();
  for (n=1; n< 7; n++)
  printf("%d\t",rand());
  return 0;
}

The output of first trial is as below

     

The result of second trial is same as that of first as given below

     

Note that the two sets of random numbers are identical. In fact every time the function rand ()is used without changing the seed number, it will produce same set of random numbers. In order to get a different single or set of random numbers a different seed number has to be provided through function srand ().Program  provides an illustration of generating one random number with the same seed numbers.

Random number generation with same seed number

#include<stdio.h>
#include<stdlib.h>
int main()
{
  unsigned seed;
  int n ;
  clrscr();
  seed = 1;
  for (n=1;n< 4;n++)
   {
     srand(seed);
     printf("%d\t",rand()) ;
   }
     printf ("\n");
     return 0;
}

The expected output is as given below

      

The output illustrates that if the seed number is 1 then the random number generated is 346. This is also called default seed number; when a user does not provide a seed number, the system provides 1 as the seed number.

When a set of random numbers is generated, for the first number, the system provides 1 as the seed number and the random number generated is used as the seed for the next random number. Program computes rand () with a seed number provided by the user of the program. Therefore, for every application of rand () a different seed number is required to produce a different random number. That is, while producing a set of random numbers, if the first random number is different, then the whole set would be different.

Illustrates random number generation with a seed number

#include<stdio.h> 
#include<stdlib.h>
int main()
{
  int n , S;
  clrscr();
  printf("Enter an integer seed number : ");
  scanf("%d", &S);
  srand(S);
  for (n=1;n<=6;n++)
  printf("%d\t", rand());
 return 0;
}

The output is as given below.

 

You’ll also like:

  1. Random Access Files
  2. Math.random() in Java Example
  3. Random Number is Chosen Between 1 and 100
  4. What is RAM (random access memory)? Definition
  5. Random Access Methods in Computer Networks
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW