| | |
program to generate random numbers for a given range
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
C++ Syntax (Toggle Plain Text)
#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; }
here is a function to do this
C++ Syntax (Toggle Plain Text)
//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; }
•
•
Join Date: Aug 2008
Posts: 1
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Tekmaven; Aug 4th, 2008 at 6:30 am. Reason: Code tags
Apropos, I far as I know,
Right (but cumbersom) construct is (alas):
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):
cpp Syntax (Toggle Plain Text)
(int)(N*(rand()/(double)MAX_RAND))
![]() |
Similar Threads
- C++ Random Number Generator (C++)
- Hello I need help on a random number generator. PLEASE (C++)
- C++ Random Numbers (C++)
- how to use generate random numbers (Visual Basic 4 / 5 / 6)
- Generating random numbers using VC6 (C++)
- Program will not generate random numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: HELP!! how do you use atoi?
- Next Thread: is c++ hard?
Views: 6687 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






