Generate symbol randomly

Hello.I'm doing my school assignment.
I want the code that to generate any two symbols from # $ X O once randomly.
For example , $X .
I have written some code but cannot generate correctly.
Please help.Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib> 
#include <ctime> 
using namespace std;

int main() {

  srand( time(0) );

  for (int i = 0; i < 2; i++){

    for ( int counter = 1; counter != 0; counter++ ) {
        char a = rand()%54 + 35;
         if ( a == 35 || a == 36 || a == 79 || a == 88)
         cout << a;
         break;
         }     
    }
return 0;
}
Last edited on
Hello VinCenT98,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

The first thing I notice is the first line in the for loop. "rand" returns an "int" which you ate trying to store in a "char". Not likely to give you the result that you want. Yo should consult an ASCII table to see where what you want resides. And if they are not all together this will be a problem. The "%54" will put you in the middle of the numbers 0 - 9 and when you add 35 this is near the end of the capital letters.

An alternative to what you want to do is define a std::string and initialize it to the characters that you want.Such as std::string symbleChars{ "#$XO" };. Then generate a random number between 0 and the length of the string. Use that number as a subscript to get a character from the string.

I think this would be easier than what you are trying to do.

Try to avoid using variable names like "a". I realize that typing one letter is easy, but use something more descriptive for what it is used for or does. At sometime in the future you will look at this program and spend about half of your time just trying to figure out what the single letter variables are for. Been there done that, just today actually, not much fun.

Looking more at the for loop i realize it would be an endless loop except for the break statement which will only let the for loop execute only one time,so the endless loop has no real meaning.

Sorry it is the inner for loop that has the problems. the outer for loop is OK. Not easy to read your code as it is.

Hope that helps,

Andy
You can use C++'s random library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <random>

class Rnd {
    std::random_device rd;  // assuming this will actually be a random device
    std::minstd_rand msr; // we will seed this generator with the random device (instead of time)
public:
    Rnd() : rd(), msr(rd()) {}
    int rnd(int n) { return msr() % n; }
};

Rnd RND;

int main() {
    using std::cout;

    for (int i = 0; i < 20; i++)
        cout << "#$XO"[RND.rnd(4)] << "#$XO"[RND.rnd(4)] << '\n';

    return 0;
}

I should add that this is just the basics. It would be more correct to use a uniform_int_distribution(0,3). But this is a good replacement for C's rand.
Last edited on
Topic archived. No new replies allowed.