Monday, 3 October 2011

use of random function




random() function is used to generate a number between 0 and n-1 if we pass a number n as a parameter to it. its header file is stdlib.h
for ex
int x = random(10) will generate any number between 0 and 9
To generate a number in a range we can use the following formula
random(UL-LL+1)+ LL
where UL = Upper Limit
          LL = Lower Limit
eg to generate a number between 100 and 500
random(401)+100
 random function is very important for board exams as questions are being asked from it


2 comments:

  1. hi sir !
    after reading the above text i understand random function(by def.),but the eg given by u for the formula is little bit confusing.
    what is the use of adding "LL" outside the function (random(UL-LL+1)+ LL).when we are getting the required result as we have to generate a no. b\w two no's ,as shown in eg.

    ReplyDelete
  2. actually random function generates a decimal value between 0 and 1 so this is required for generating a random number in a range

    ReplyDelete