Social Icons

14 June 2012

0 Generating random number in C++ - PART -2

random() and randomize() are two other macros defined in <stdlib.h> having functioning as rand() and srand() respectively.
random(num) generate a random number within range 0 and num-1.  To genarate random numbers within a specific range (L to U) through random(), change random() to random(U-L+1)+L.

randomize() initializes the random number generator with a random number. To use randomize(), include <Time.h>.
Example:-
#include<stdio.h>
#include<iostream.h>
int main()
{
  randomize();
  int num,randnum;
  cout<<"Enter a number:\n";
  cin>>num;
  randnum=random(num)+7;
  for(int i=1;i<=randnum;i++)
   cout<<i<<"\n";
  getch();
  return 0;
}

0 comments:

Post a Comment

Hey this is a test comment

Related Posts Plugin for WordPress, Blogger...