Having trouble with rand();

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 3
Reputation: #include<James> is an unknown quantity at this point 
Solved Threads: 0
#include<James> #include<James> is offline Offline
Newbie Poster

Having trouble with rand();

 
0
  #1
Jan 10th, 2009
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. srand(time(0));
  10. int number;
  11. number = 2;
  12. while(2 == number) {
  13. int random;
  14. random = rand() % 2;
  15. string alpha[3];
  16. alpha[0] = "a";
  17. alpha[1] = "b";
  18. alpha[2] = "c";
  19.  
  20. string list[3];
  21. int ran;
  22. ran = rand() % 3;
  23. list[0] = "1";
  24. list[1] = "2";
  25. list[2] = "3";
  26. string choose[2];
  27. choose[0] = 'alpha';
  28. choose[1] = 'list';
  29. cout << choose[random];
  30.  
  31.  
  32. system("PAUSE");
  33.  
  34.  
  35.  
  36. };
  37. };
My program as seen above runs just fine for me, except for one small yet crippling bug. The random character that comes out of this program is not from either of my list's! It is always either t or h. Why does this happen? Thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Having trouble with rand();

 
0
  #2
Jan 10th, 2009
Coming from python are we?

In C++, the quotes (") and (') are NOT interchangable.

The following lines in your code:
  1. choose[0] = 'alpha';
  2. choose[1] = 'list';
Generated the following warning in my compiler:
  1. rand.cpp(27) : error C2015: too many characters in constant
  2. rand.cpp(27) : error C2593: 'operator =' is ambiguous
When I changed those lines to use (")

The compiler complained about cout << choose[random];
  1. rand.cpp(29) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
so I changed it to cout << choose[random].c_str()
This is the output from my sample run:
  1. alphaPress any key to continue . . .
  2. listPress any key to continue . . .
  3. alphaPress any key to continue . . .
  4. listPress any key to continue . . .
  5. listPress any key to continue . . .
  6. alphaPress any key to continue . . .
  7. listPress any key to continue . . .
  8. listPress any key to continue . . . ^C

(I don't like infinite loops, it would have been nice for you to have an actual exit condition.)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 953
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Having trouble with rand();

 
0
  #3
Jan 10th, 2009
Do not declare things in a loop. Do it all before hand.
I think you're confusing std::string with a C style string, because you can just append things to the end without dealing with [] . Unless you plan on allocating them(no need with STL containers like vector).
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: #include<James> is an unknown quantity at this point 
Solved Threads: 0
#include<James> #include<James> is offline Offline
Newbie Poster

Re: Having trouble with rand();

 
0
  #4
Jan 10th, 2009
Murtan, im actually comming from perl, thanks for trying although I wanted the program to choose a random element from one of the two lists. If I had wanted what your version gave me I couldve used the "". The reason I did not include an exit to the loop is because it is a sample program, and it is meant simply for showing you the bug. Once again thanks for trying. Mosaic, thanks for the tip about declaring stuff before the loop, although the second part of your post I believe is directed towards Murtan. Im still tinkering with it to find a solution, if you find one please share.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 953
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Having trouble with rand();

 
0
  #5
Jan 10th, 2009
It was towards you. Because you're trying to create an array of std::strings, which you don't need to. You can just dynamically append() data to a string instead of splitting it over arrays of strings(which you should allocate first). If you need a lot of strings like in choose use a container.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Having trouble with rand();

 
0
  #6
Jan 10th, 2009
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int number = 2;
  10. int random;
  11. int ran;
  12. string alpha[3];
  13. string list[3];
  14. string choose[2];
  15.  
  16. srand(time(0));
  17.  
  18. while(2 == number) {
  19. random = rand() % 2;
  20. string alpha[3];
  21. alpha[0] = "a";
  22. alpha[1] = "b";
  23. alpha[2] = "c";
  24.  
  25. ran = rand() % 3;
  26. list[0] = "1";
  27. list[1] = "2";
  28. list[2] = "3";
  29. choose[0] = "alpha";
  30. choose[1] = "list";
  31.  
  32. if (choose[random] == "alpha") {
  33. cout << "get random alpha" << endl;
  34. cout << alpha[ran] << endl;
  35. } else {
  36. cout << "get random list" << endl;
  37. cout << list[ran] << endl;
  38. }
  39. }
  40. }
Last edited by Comatose; Jan 10th, 2009 at 4:04 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Having trouble with rand();

 
0
  #7
Jan 10th, 2009
It would appear (from the code and our following discussion) that you want to randomly select a character from one of the two lists.

Lets go just a bit higher level, one list appears to be letters and the other appears to be digits.

Are you looking for a 'two-level' selection where we first select letters or digits and then select one of those?

Or would a 'one-level' select one from the set of letters and digits be sufficient?

For the first you could do something like:
  1. char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  2. char digit[] = "0123456789";
  3.  
  4. char generated;
  5. if (rand() % 2 == 0)
  6. generated = alpha[rand() % strlen(alpha)];
  7. else
  8. generated = digit[rand() % strlen(digit)];

The second would be similar, but only use one rand() call:
  1. char genset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  2. char generated = genset[rand() % strlen(genset)];

Is that useful at all to what you're trying to do?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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