Question:
How to get random symbols in the C++ programming language.?
?
2010-02-24 00:33:56 UTC
I am using a program that is all about math and generates random numbers, but for one section I need to generate random symbols in addition to the random numbers, I have no problems with the language itself I just cannot seem to generate random symbols such as the addition symbol subtraction and so on, if anyone can help I would be very greatful!
Four answers:
Nick T
2010-02-24 12:45:27 UTC
I assume you mean generate a random numeric operator.

There are several different ways you could accomplish this, In order of simplicity/naivety

1) Use an IF/ELSE IF construct to select an appropriate item based on a random number.

2) Use a switch statement based on a random number.

3) create a character string which contains all of the required operators i.e. "+-/*" then generate a random number as an index into the string
dolchio
2010-02-24 01:45:22 UTC
I'm assuming you mean symbols in the ASCII character set. You can use rand() for this as well. Just get your program to generate a number between 0 and 128 (i.e. the total number of ASCII characters) and cast it into a char by typing



char symbol;



symbol = rand() % 128;



the number will be interpreted as the ASCII value for a character when stored.



Oh, and I don't know if you've already done this but if you seed your random number generator with the current time you can make it generate a different set of random numbers on each run.



srand(time(NULL));



Good Luck.
venzon
2016-10-22 02:24:44 UTC
You do look to invite extremely some questions approximately this talk board that each and each provide the effect they're homework assignments... You disguise the certainty (that they are homework assignments) nicely, inspite of the undeniable fact that! whats up, here is an theory... Why do you pass decrease back to the different questions you have asked, and choose a terrific answer? people have long gone throughout the attempt of assisting you, why do no longer you pass throughout the attempt of effective them?
2010-02-24 00:43:41 UTC
Try

#include // Include rand()



using namespace std; // Make rand() visible



int a = rand(); // rand is a standard function that all compilers have


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...