| | |
Random no. function dispays the same number
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 147
Reputation:
Solved Threads: 7
hi
My random number function is meant to loop 6 times & output 6 different numbers. But instead it outputs 6 of the same numbers. How do I fix it?
Any advice would be really helpful.
My random number function is meant to loop 6 times & output 6 different numbers. But instead it outputs 6 of the same numbers. How do I fix it?

Any advice would be really helpful.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> #include <iomanip> #include <string> using namespace std; int random(int max) { srand(time(NULL)); return ((rand()%max)+1); } int main() { for (int i=0; i<6; i++) { used_nos[i] = random(40); cout << used_nos[i] << endl; } return 0; }
Last edited by gretty; Aug 2nd, 2009 at 5:05 am.
I'd imagine if you were to wait in the for loop for about a second per iteration it'd be different.
The problem is that you're reseeding the rand function so quickly it's being reseeded off the same value. You only need to seed it once, so do it at the start of main.
For further clarification... srand takes time(null) in your code, which updates every second, I believe. So if srand was in the function and if the loop executed for over a second you'd get different numbers out every different second.
The problem is that you're reseeding the rand function so quickly it's being reseeded off the same value. You only need to seed it once, so do it at the start of main.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> #include <iomanip> #include <string> using namespace std; int random(int max) { return ((rand()%max)+1); } int main() { srand(time(NULL)); for (int i=0; i<6; i++) { used_nos[i] = random(40); cout << used_nos[i] << endl; } return 0; }
For further clarification... srand takes time(null) in your code, which updates every second, I believe. So if srand was in the function and if the loop executed for over a second you'd get different numbers out every different second.
Last edited by twomers; Aug 2nd, 2009 at 5:32 am.
![]() |
Similar Threads
- Problem with random function (C++)
- help in function to return the number of items greater than certain number (C++)
- Arrays, Strings, ConCat??? Please help (C++)
- Calling a random function (Python)
- Array with random function (C++)
- Select a no. using random function form a selcted numbers (C++)
- Random Function Error (Python)
- help with hangman program (Java)
- random function generator (C)
- Need to know how to create a database that will generate a random number (Database Design)
Other Threads in the C++ Forum
- Previous Thread: Exceptions handling problems
- Next Thread: Mp3 bitstream
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






