i tried the following coding .it compiled without errors but gave absurd results during runtime. can u pls help me with it?

#include<iostream.h>
#include<stdlib.h>
 int main()
{
  int i;
  cout<<"ten random numbers for the range 0 to 50"<<endl;
  for(i=0;i<10;i++)
 {
   cout<<rand()<<endl;
  return 0;
}
}

Recommended Answers

All 16 Replies

i tried the following coding .it compiled without errors but gave absurd results during runtime. can u pls help me with it?
#include<iostream.h>
#include<stdlib.h>
int main()
{
int i;
cout<<"ten random numbers for the range 0 to 50"<<endl;
for(i=0;i<10;i++)
{
cout<<rand()<<endl;
return 0;
}
}

You probably want the "return 0" AFTER the for-loop is over. When it hits that, it'll just end the program. Moving that line so that it was AFTER the for-loop gave me these results:

ten random numbers for the range 0 to 50
41
18467
6334
26500
19169
15724
11478
29358
26962
24464

Note that you are not seeding the random number generator with the srand function, so don't be surprised if you get the same numbers every time.

Well i have seen your code. It actually can create random numbers from the whole integer set.

Therefore i have just edited the whole program and made it such that it will get random numbers from 0 to 50.

Please dont copy the code but, Check out how the loop actually is running and implement it on your own.

#include<iostream>

using namespace std;

int randomnumber();
int main()
{
    int i;
    cout<<"ten random numbers for the range 0 to 50"<<endl;
    for(i=0;i<10;i++)
    {
      randomnumber();
            
    }
    cin.get();
}

int randomnumber()
{
    int j;
    j = rand();
      if (!(j >= 50))
      {
          if (!(j<=0))
          {
                      cout<< j<<"\n";
          }
      }
      else {randomnumber();}
}

Hope your problem is solved with this.
However there is another problem . Some times the random numbers may be the same. I mean there can be two 28 or any other number coming out. Try avoiding it by using an array.

Consider using the % operator.

Recursion in this instance is very poor, because there is no clear exit condition and it may take a very long time (and thus a large amount of stack - perhaps too much) before you get the result you want.

So instead of Recursion i should go on getting the remainder by dividing the number with some value like 50 or so?

I usually initialize by using srand() function

srand ( time(NULL) );

time() is in the header <ctime> or <time.h> i am not very sure.

To generate random numbers from 0-50 use the % operator

int random_numer= rand()%50;

The function rand() produced integers between 0 and a large (system-dependent) integer called RAND_MAX. To get integers between 0 and 50, write:

cout << 50*rand()/RAND_MAX << endl;

hi i need to generate numbers in the given range....here i have lower as well as upper limit...
how do i dp that...
thanks

int number = rand() % N;   // gives you a random number from 0..(N-1)

number += L;     // the number between L..(L+N-1)

So if you know your upper and lower limits, you can work out what N & L should be.

thank you...
int lower limit,upper limit;
int range=upper limit-lower limit;
int number;
for(int i=0;i<20;i++)
{
number=lower limit+(rand()%range);
cout<<number<<endl;
}
thats what i used in my code....

umm.... I'm not sure that's quite correct...

as a simple example, if lower_limit = 0 and upper_limit = 2, what is range?? keep in mind that the numbers in the range will be 0,1 or 2.

If you wanna stick to your code, its fine. Just make some modification for an elegant solution. Here's my advice. I prefer to seed the random number generator first, then use the modulus (%) function to limit the generated number.

#include<iostream.h>
#include<stdlib.h>
#include<time.h>
int main()
{
     int i;
     cout<<"ten random numbers for the range 0 to 50"<<endl;
     srand(time(NULL));

     for(i=0;i<10;i++)
     {
          cout << rand() % 51 << endl;            // number of 0 to 50
     }

     return 0;
}

Thank you dougy and zeah.
It solved my one of the biggest security vulnerability.
It is not related to random numner, but I wanted a random generation function to fix the security issue.

Regards,
Tukaram

here is a function to do this

//limi is the inferior limit of the range
//lims is the superior limit of the range

int random( int limi, int lims )
{
	return ( rand() % ( lims - limi ) ) + limi;
}

I am new to C++. The computer has a timer that you have to tap into. Try this program and adjust it to your numbers range.

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

int main( )
{  srand(time(NULL));
   cout << "Draw lotto numbers: Please enter (y or n) lower case only:";
   string resp;
cin >> resp;
while(resp == "y")
{  int x = rand() % 59 + 1;
   int y = rand() % 59 + 1;
   int z = rand() % 59 + 1;
   int q = rand() % 59 + 1;
   int p = rand() % 59 + 1;
   int u = rand() % 59 + 1;
   cout << x << " " << y << " " << z << " " << q << " " << p << " " << u << endl;
   //if (x == y) 
     // cout << "prize\n";
   cout << "Do you want draw more lotto numbers (y/n)? lower case only:";
   cin >> resp;
}

Apropos, I far as I know, rand() % N (where rand() is a pseudo-random numbers generator) is NOT uniformely distributed in 0..N-1 range...

Right (but cumbersom) construct is (alas):

(int)(N*(rand()/(double)MAX_RAND))
commented: You are correct about it not being uniform. +5
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{int i,j,count=1,a[50],n,p;
clrscr();
for(i=1;count<=50;i++)
{j=rand();
if(!(j<=0)&&!(j>50))
{p=1;
for(n=0;n<count-1;n++)
  if(a[n]==j)
   p=0;

 if(p!=0)
{ a[count-1]=j;
  cout<<j<<endl;
    ++count;
}
}
}
getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.