Home › Forums › C Programming › Beginner Help
- This topic has 4 replies, 2 voices, and was last updated 17 years, 3 months ago by S.Thananchayan.
- AuthorPosts
- July 20, 2007 at 4:13 pm #1989chuckyParticipant
Hi, I’m new here and I’m also new to C++. And I mean literally new, as in I just started yesterday. I’ve read a few tutorials and learned somethings but I still know basically nothing. I’m making some simple programs to practice and the current one is a random number generator.
Here is the code.123456789101112131415161718192021222324252627282930#include <iostream><br />using namespace std;<br />int main()<br /><br />{<br />int a,b,c,d,e,f;<br />string g,h="a";<br />loop:<br />a = (rand()%10);<br />b = (rand()%10);<br />c = (rand()%10);<br />d = (rand()%10);<br />e = (rand()%10);<br />f = (rand()%10);<br />cout<<"n";<br />cout << a;<br />cout << b;<br />cout << c;<br />cout << d;<br />cout << e;<br />cout << f;<br />cout<<"n";<br />cout<<"n";<br />cout<<"To create a new random number, press a and then enter.";<br />getline (cin,g);<br />if (g==h){<br />goto loop;<br />}<br />cin.get();<br />}</iostream>If something looks dumb, I’ve already told you I only started yesterday. But the questions are instead of entering a to repeat, is there anyway to just push enter and have it repeat;and whenever I run this program, it will generate a random number, but every time I run it the random numbers are the same. For example this first random number I generate might be 134789 and the second might be 983038, which are completely random. But if you run the program again, the same numbers come up. Why does this happen and how do I fix it.Thanks!
- July 27, 2007 at 7:33 am #3240S.ThananchayanParticipant
You need loop to hit random off system time
1234567891011121314151617181920212223242526272829303132#include <iostream><br />#include <ctime><br />using namespace std;<br />int main()<br />{<br />int a,b,c,d,e,f;<br />string g,h="a";<br />srand(time(0));<br />loop:<br />a = (rand()%10);<br />b = (rand()%10);<br />c = (rand()%10);<br />d = (rand()%10);<br />e = (rand()%10);<br />f = (rand()%10);<br />cout<<"n";<br />cout << a;<br />cout << b;<br />cout << c;<br />cout << d;<br />cout << e;<br />cout << f;<br />cout<<"n";<br />cout<<"n";<br />cout<<"To create a new random number, press a and then enter.";<br />getline (cin,g);<br />if (g==h){<br />goto loop;<br />}<br />cin.get();<br />}<br /></ctime></iostream> - July 27, 2007 at 9:15 am #3241chuckyParticipant
Thanks, it worked. What did the function you added do?
- August 11, 2007 at 11:01 am #3242chuckyParticipant
Oh, so no one wants to help the noob? Come on people. What did whatever he added do?
- August 13, 2007 at 11:15 am #3243S.ThananchayanParticipant
Unless something random seeds Rand. Rand will generate the same random numbers every time. In this case were using your computers time.
And every time you loop your system time is different So your numbers from say 3:43:01 to 3:43:02 will be different. Random enough for most applications.
Read Beginning C++ Game Programming
· ISBN-10: 1592002056
· ISBN-13: 978-1592002054
That’s where I learned that
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.