Home » Ask & Discuss » Board Exams - CBSE, ICSE, State Boards » Computer Science « Back to Discussion



Computer Science

Manotapa Bhaumik's Avatar
Cool goIITian

Joined: 14 Apr 2008
Post: 55
22 Apr 2008 21:58:50 IST
0 People liked this
3
442 View Post
C++
None

How to use randomizer funtion to generate random nos?


Share this article on:

Comments (3)

Sagar  Vaze's Avatar

Blazing goIITian

Joined: 11 Apr 2008
Posts: 315
23 Apr 2008 17:45:41 IST
0 people liked this

use rand function like

int a=rand()

to get numbers in a range say 0 to 9

int a=rand() % 10

you need cstdlib for this
Selva Ganesh's Avatar

Hot goIITian

Joined: 21 Feb 2007
Posts: 165
23 Apr 2008 22:21:22 IST
0 people liked this

int x = rand(n);

the above function generates a random number from
0 to (n-1)

The function is defined under "stdlib.h"
Suresh Kumar's Avatar

Forum Expert
Joined: 24 Aug 2009
Posts: 1342
23 Sep 2009 21:36:54 IST
0 people liked this

 hello dear

The C++ standard library includes a pseudo random number generator for generating random numbers. In order to use it we need to include the <cstdlib> header. To generate a random number we use the rand() function. 

here i will generate a single random number:

#include <cstdlib> 

#include <iostream>

 using namespace std;

 int main() 

    int random_integer = rand(); 

    cout << random_integer << endl; 

}




Quick Reply


Reply

Some HTML allowed.
Keep your comments above the belt or risk having them deleted.
Signup for a avatar to have your pictures show up by your comment
If Members see a thread that violates the Posting Rules, bring it to the attention of the Moderator Team
Free Sign Up!

Preparing for IIT-JEE ?

Arihant Revision Package for IIT JEE - Books, Practice Tests + Rank Predictor


@ INR 1,995/-

For Quick Info

Name

Mobile No.

Find Posts by Topics

Physics.

Topics

Mathematics.

Chemistry.

Biology

Parents

Board

Fun Zone

Sponsored Ads