943,584 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1014
  • C++ RSS
Jun 22nd, 2008
0

Random number sequences

Expand Post »
I know about using ran() to create a random number between x and y, however i am creating a banking program and i am wondering how to randomly generate a 17 digit pin, then store the pin in an outside file, then have the program check the file for the pin
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Superfat is offline Offline
10 posts
since Jun 2008
Jun 22nd, 2008
0

Re: Random number sequences

c++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <string>
  3.  
  4. inline char random_digit() { return '0' + std::rand() % 10 ; }
  5.  
  6. inline std::string random_pin( std::size_t ndigits )
  7. {
  8. std::string pin ;
  9. for( std::size_t i = 0 ; i < ndigits ; ++i ) pin += random_digit() ;
  10. return pin ;
  11. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Jun 22nd, 2008
0

Re: Random number sequences

I tried it, and recieved an error,

C++ Syntax (Toggle Plain Text)
  1. [Linker error] undefined reference to `WinMain@16'
  2.  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Superfat is offline Offline
10 posts
since Jun 2008
Jun 22nd, 2008
0

Re: Random number sequences

vijayan121 has just provided the functions, you'll have to adapt them according to your main code and use them. His functions will compile, but how do you expect them to run?!
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Jun 22nd, 2008
0

Re: Random number sequences

Click to Expand / Collapse  Quote originally posted by Superfat ...
I tried it, and recieved an error,

C++ Syntax (Toggle Plain Text)
  1. [Linker error] undefined reference to `WinMain@16'
  2.  
You are trying to build a Win 32 GUI program and you have no WinMain(...) - hence the error.
If you want to build a GUI program, then write the WinMain(...) too, see
http://msdn.microsoft.com/en-us/library/ms633559.aspx

Otherwise, start a new Win32 Console Application project, which uses the int main() .
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jun 22nd, 2008
0

Re: Random number sequences

Okay, i tried something that i thought up, reading up i remembered that a 17 digit number isnt really possible in C++... so i have come up with this, however i am getting two errors, i think it has something to do with the brackets... but i cant figure anything out

C++ Syntax (Toggle Plain Text)
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. int main(int nNumberofArgs, char* pszArgs[])
  8. {
  9. srand ( (unsigned)time ( 0 ) );
  10.  
  11. const int a = rand() % 9 ;
  12. const int b = rand() % 9 ;
  13. const int c = rand() % 9 ;
  14. const int d = rand() % 9 ;
  15. const int e = rand() % 9 ;
  16. const int f = rand() % 9 ;
  17. const int g = rand() % 9 ;
  18. const int h = rand() % 9 ;
  19. const int i = rand() % 9 ;
  20. const int j = rand() % 9 ;
  21. const int k = rand() % 9 ;
  22. const int l = rand() % 9 ;
  23. const int m = rand() % 9 ;
  24. const int n = rand() % 9 ;
  25. const int o = rand() % 9 ;
  26. const int p = rand() % 9 ;
  27. const int q = rand() % 9 ;
  28. const int r = rand() % 9 ;
  29. {
  30. char transactionType;
  31. cout << "Press N to cancel, and Y to continue:\n " << endl;
  32. cin >> transactionType;
  33.  
  34. if (transactionType == 'x' ||
  35. transactionType == 'X')
  36. {
  37. break;
  38. }
  39.  
  40. if (transactionType == 'y' ||
  41. transactionType == 'y')
  42. {
  43.  
  44. continue ;
  45.  
  46. }
  47. }
  48. {
  49. cout << "abcdefghijklmnopqr";
  50. }
  51. }

errors:
C++ Syntax (Toggle Plain Text)
  1. break statement not within
  2. continue statement not within
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Superfat is offline Offline
10 posts
since Jun 2008
Jun 22nd, 2008
0

Re: Random number sequences

C++ Syntax (Toggle Plain Text)
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. int main ( int nNumberofArgs, char* pszArgs[] )
  8. {
  9. srand ( ( unsigned ) time ( 0 ) );
  10.  
  11. const int a = rand() % 9 ;
  12. const int b = rand() % 9 ;
  13. const int c = rand() % 9 ;
  14. const int d = rand() % 9 ;
  15. const int e = rand() % 9 ;
  16. const int f = rand() % 9 ;
  17. const int g = rand() % 9 ;
  18. const int h = rand() % 9 ;
  19. const int i = rand() % 9 ;
  20. const int j = rand() % 9 ;
  21. const int k = rand() % 9 ;
  22. const int l = rand() % 9 ;
  23. const int m = rand() % 9 ;
  24. const int n = rand() % 9 ;
  25. const int o = rand() % 9 ;
  26. const int p = rand() % 9 ;
  27. const int q = rand() % 9 ;
  28. const int r = rand() % 9 ;
  29. {
  30. char transactionType;
  31. cout << "Press N to cancel, and Y to continue:\n " << endl;
  32. cin >> transactionType;
  33.  
  34. if ( transactionType == 'x' ||
  35. transactionType == 'X' )
  36. {
  37. break;
  38. }
  39.  
  40. if ( transactionType == 'y' ||
  41. transactionType == 'y' )
  42. {
  43. continue ;
  44. }
  45. }
  46. {
  47. cout << "abcdefghijklmnopqr";
  48. }
  49. }


Most of your program doesn't make any sense. Should I elaborate or do you wanna look at a newbie tutorial?
Last edited by iamthwee; Jun 22nd, 2008 at 4:04 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 22nd, 2008
0

Re: Random number sequences

Elaborate please =], i was trying to say that a-r are random numbers between 1 and 9, this is just a snipet of some code i already have, i an just having some problems with figuring out random numbers... i figured that if the largest variable is an unsigned, max of about 4 billion, and seing as i need a 17 digit number, i generate a number for each digit, when the person continues it prints out each digit in the end
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Superfat is offline Offline
10 posts
since Jun 2008
Jun 22nd, 2008
1

Re: Random number sequences

Click to Expand / Collapse  Quote originally posted by Superfat ...
Elaborate please =], i was trying to say that a-r are random numbers between 1 and 9, this is just a snipet of some code i already have, i an just having some problems with figuring out random numbers... i figured that if the largest variable is an unsigned, max of about 4 billion, and seing as i need a 17 digit number, i generate a number for each digit, when the person continues it prints out each digit in the end

Well dear Superfat, the most effective solution for you problem has already been given by vijayan121. Unfortunately you aren't able to understand his fine function. So GOD's advice should be seriously followed.

Btw, your nice random numbers a..r never contain 9, got it?

krs,
tesu
Last edited by tesuji; Jun 22nd, 2008 at 4:39 pm.
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: student needing guidance on final exam code
Next Thread in C++ Forum Timeline: storing a "word" within a sentence





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


Follow us on Twitter


© 2011 DaniWeb® LLC