943,545 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 14787
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 30th, 2008
0

program to generate random numbers for a given range

Expand Post »
i tried the following coding .it compiled without errors but gave absurd results during runtime. can u pls help me with it?
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bramprakash1989 is offline Offline
9 posts
since Mar 2008
Mar 30th, 2008
0

Re: program to generate random numbers for a given range

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:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Mar 30th, 2008
0

Re: program to generate random numbers for a given range

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Mar 30th, 2008
0

Re: program to generate random numbers for a given range

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 30th, 2008
0

Re: program to generate random numbers for a given range

So instead of Recursion i should go on getting the remainder by dividing the number with some value like 50 or so?
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Mar 30th, 2008
0

Re: program to generate random numbers for a given range

I usually initialize by using srand() function

c++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  1. int random_numer= rand()%50;
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Apr 1st, 2008
0

Re: program to generate random numbers for a given range

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

C++ Syntax (Toggle Plain Text)
  1. cout << 50*rand()/RAND_MAX << endl;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RobertBruce is offline Offline
1 posts
since Apr 2008
Apr 5th, 2008
0

Re: program to generate random numbers for a given range

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeah is offline Offline
8 posts
since Mar 2008
Apr 5th, 2008
0

Re: program to generate random numbers for a given range

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Apr 6th, 2008
-1

Re: program to generate random numbers for a given range

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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
zeah is offline Offline
8 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: HELP!! how do you use atoi?
Next Thread in C++ Forum Timeline: instancing a class with fstream class variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC