program to generate random numbers for a given range

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 9
Reputation: bramprakash1989 is an unknown quantity at this point 
Solved Threads: 0
bramprakash1989 bramprakash1989 is offline Offline
Newbie Poster

program to generate random numbers for a given range

 
0
  #1
Mar 30th, 2008
i tried the following coding .it compiled without errors but gave absurd results during runtime. can u pls help me with it?
  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. int main()
  4. {
  5. int i;
  6. cout<<"ten random numbers for the range 0 to 50"<<endl;
  7. for(i=0;i<10;i++)
  8. {
  9. cout<<rand()<<endl;
  10. return 0;
  11. }
  12. }
Last edited by Ancient Dragon; Mar 30th, 2008 at 7:37 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: program to generate random numbers for a given range

 
0
  #2
Mar 30th, 2008
Originally Posted by bramprakash1989 View Post
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:
  1. ten random numbers for the range 0 to 50
  2. 41
  3. 18467
  4. 6334
  5. 26500
  6. 19169
  7. 15724
  8. 11478
  9. 29358
  10. 26962
  11. 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.
Last edited by VernonDozier; Mar 30th, 2008 at 4:15 am. Reason: Edited grammar to make post more clear.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: program to generate random numbers for a given range

 
0
  #3
Mar 30th, 2008
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.

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int randomnumber();
  6. int main()
  7. {
  8. int i;
  9. cout<<"ten random numbers for the range 0 to 50"<<endl;
  10. for(i=0;i<10;i++)
  11. {
  12. randomnumber();
  13.  
  14. }
  15. cin.get();
  16. }
  17.  
  18. int randomnumber()
  19. {
  20. int j;
  21. j = rand();
  22. if (!(j >= 50))
  23. {
  24. if (!(j<=0))
  25. {
  26. cout<< j<<"\n";
  27. }
  28. }
  29. else {randomnumber();}
  30. }

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.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: program to generate random numbers for a given range

 
0
  #4
Mar 30th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: program to generate random numbers for a given range

 
0
  #5
Mar 30th, 2008
So instead of Recursion i should go on getting the remainder by dividing the number with some value like 50 or so?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: program to generate random numbers for a given range

 
0
  #6
Mar 30th, 2008
I usually initialize by using srand() function

  1. 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

  1. int random_numer= rand()%50;
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1
Reputation: RobertBruce is an unknown quantity at this point 
Solved Threads: 0
RobertBruce RobertBruce is offline Offline
Newbie Poster

Re: program to generate random numbers for a given range

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

  1. cout << 50*rand()/RAND_MAX << endl;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: zeah is an unknown quantity at this point 
Solved Threads: 0
zeah zeah is offline Offline
Newbie Poster

Re: program to generate random numbers for a given range

 
0
  #8
Apr 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: program to generate random numbers for a given range

 
0
  #9
Apr 5th, 2008
  1. int number = rand() % N; // gives you a random number from 0..(N-1)
  2.  
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: zeah is an unknown quantity at this point 
Solved Threads: 0
zeah zeah is offline Offline
Newbie Poster

Re: program to generate random numbers for a given range

 
0
  #10
Apr 6th, 2008
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....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC