Random numbers from interval (a;b), (0,b), and real numbers.

C++
voted: 0
vote
c++

This is my code which shows you random numbers from 3 loops (for, do while and while). First one (for) is showing n numbers from interval (0;b) Second one (do while) is showing n numbers from interval (a;b) and this numbers are real numbers. Third one (while) is showing n numbers from interval (a;b).

#include <conio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int a,b,i,n,d;
    float d1;
    srand(time(NULL));
    cout<<"Get a: ";
    cin>>a;
    cout<<"Get b: ";
    cin>>b;
    cout<<"Get n: ";
    cin>>n;
    cout<<"Loop for"<<endl;
    for(i = 0; i < n; i++)
    {
          d = (rand()%(b+1));
          cout<<d<<" ";
    }
    cout<<endl;
    cout<<"Loop do - while"<<endl;
    i = 0;
    do{
          d1 = a + ((float)rand()/(RAND_MAX))*(b-a);
          cout<<d1<<" ";
          i++;
    }
    while(i < n);
    cout<<endl;
    cout<<"Loop while"<<endl;
    i = 0;
    while(i < n)
    {
             d = a +(rand()%((b-a)+1));
             cout<<d<<" ";
             i++;
    }
    cout<<endl;
    cout<<"End";
    getch();
    return 0;
}
working url: not entered
version: not entered
Leave a Comment:
Characters left:
No comments yet.