program to generate random numbers for a given range

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

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
  #11
Apr 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 34
Reputation: chiwawa10 is an unknown quantity at this point 
Solved Threads: 5
chiwawa10's Avatar
chiwawa10 chiwawa10 is offline Offline
Light Poster

Re: program to generate random numbers for a given range

 
0
  #12
Apr 6th, 2008
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.

  1. #include<iostream.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int main()
  5. {
  6. int i;
  7. cout<<"ten random numbers for the range 0 to 50"<<endl;
  8. srand(time(NULL));
  9.  
  10. for(i=0;i<10;i++)
  11. {
  12. cout << rand() % 51 << endl; // number of 0 to 50
  13. }
  14.  
  15. return 0;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: tukarampl is an unknown quantity at this point 
Solved Threads: 0
tukarampl tukarampl is offline Offline
Newbie Poster

Re: program to generate random numbers for a given range

 
0
  #13
Aug 3rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 22
Reputation: NickyU is an unknown quantity at this point 
Solved Threads: 0
NickyU's Avatar
NickyU NickyU is offline Offline
Newbie Poster

Re: program to generate random numbers for a given range

 
0
  #14
Aug 3rd, 2008
here is a function to do this
  1.  
  2. //limi is the inferior limit of the range
  3. //lims is the superior limit of the range
  4.  
  5. int random( int limi, int lims )
  6. {
  7. return ( rand() % ( lims - limi ) ) + limi;
  8. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: Brian80 is an unknown quantity at this point 
Solved Threads: 0
Brian80 Brian80 is offline Offline
Newbie Poster

Re: program to generate random numbers for a given range

 
0
  #15
Aug 3rd, 2008
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.

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. #include<string>
  5.  
  6. using namespace std;
  7.  
  8. int main( )
  9. { srand(time(NULL));
  10. cout << "Draw lotto numbers: Please enter (y or n) lower case only:";
  11. string resp;
  12. cin >> resp;
  13. while(resp == "y")
  14. { int x = rand() % 59 + 1;
  15. int y = rand() % 59 + 1;
  16. int z = rand() % 59 + 1;
  17. int q = rand() % 59 + 1;
  18. int p = rand() % 59 + 1;
  19. int u = rand() % 59 + 1;
  20. cout << x << " " << y << " " << z << " " << q << " " << p << " " << u << endl;
  21. //if (x == y)
  22. // cout << "prize\n";
  23. cout << "Do you want draw more lotto numbers (y/n)? lower case only:";
  24. cin >> resp;
  25. }
Last edited by Tekmaven; Aug 4th, 2008 at 6:30 am. Reason: Code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: program to generate random numbers for a given range

 
0
  #16
Aug 4th, 2008
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):
  1. (int)(N*(rand()/(double)MAX_RAND))
Reply With Quote Quick reply to this message  
Reply

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




Views: 6687 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC