Random number sequences

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 10
Reputation: Superfat is an unknown quantity at this point 
Solved Threads: 0
Superfat Superfat is offline Offline
Newbie Poster

Random number sequences

 
0
  #1
Jun 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Random number sequences

 
0
  #2
Jun 22nd, 2008
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 10
Reputation: Superfat is an unknown quantity at this point 
Solved Threads: 0
Superfat Superfat is offline Offline
Newbie Poster

Re: Random number sequences

 
0
  #3
Jun 22nd, 2008
I tried it, and recieved an error,

  1. [Linker error] undefined reference to `WinMain@16'
  2.  
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Random number sequences

 
0
  #4
Jun 22nd, 2008
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?!
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Random number sequences

 
0
  #5
Jun 22nd, 2008
Originally Posted by Superfat View Post
I tried it, and recieved an error,

  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() .
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 10
Reputation: Superfat is an unknown quantity at this point 
Solved Threads: 0
Superfat Superfat is offline Offline
Newbie Poster

Re: Random number sequences

 
0
  #6
Jun 22nd, 2008
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

  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:
  1. break statement not within
  2. continue statement not within
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Random number sequences

 
0
  #7
Jun 22nd, 2008
  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 10
Reputation: Superfat is an unknown quantity at this point 
Solved Threads: 0
Superfat Superfat is offline Offline
Newbie Poster

Re: Random number sequences

 
0
  #8
Jun 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 296
Reputation: tesuji is on a distinguished road 
Solved Threads: 42
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: Random number sequences

 
1
  #9
Jun 22nd, 2008
Originally Posted by Superfat View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC