This is my first attempt at writing a c++ program, so bear with me. I tried to use as many methods as I learned in about a week-long period.
#include <iostream>
#include <stdlib.h>
#include <time.h>
void generator(char tree, char water, char land)
{
const char tileList[] = {tree, water, land};
int tileIndex = rand() % 3;
std::cout << tileList[tileIndex] << " ";
}
int main()
{
srand(time(NULL));
while(true)
{
generator('T', '~', '.');
}
}
I took out comments to improve code readability. What can be improved? What needs to be changed?
-
5\$\begingroup\$ I didn't know Putin could code... \$\endgroup\$Simon Forsberg– Simon Forsberg2014年08月30日 16:50:50 +00:00Commented Aug 30, 2014 at 16:50
-
\$\begingroup\$ @SimonAndréForsberg he has already too many distractions to be coding properly, although I'm sure he could pick it up if he wanted to ;-) \$\endgroup\$TemplateRex– TemplateRex2014年08月30日 19:21:31 +00:00Commented Aug 30, 2014 at 19:21
-
\$\begingroup\$ Just out of curiosity, why the downvote? \$\endgroup\$Vladimir Putin– Vladimir Putin2014年08月30日 19:33:14 +00:00Commented Aug 30, 2014 at 19:33
2 Answers 2
This won't compile:
void generator(char tree, char water, char land); // ... generator("T", "~", ".");
Because generator
takes 3 char
s, and you're giving it const char*
instead.
Of course that's easy to fix, just make the params char
s:
generator('T', '~', '.');
It would be better if the generator
generated, instead of printing.
It's to preserve the single responsibility principle.
Like this:
char generator(char tree, char water, char land)
{
const char tileList[] = {tree, water, land};
int tileIndex = rand() % 3;
return tileList[tileIndex];
}
int main()
{
srand(time(NULL));
while(true)
{
std::cout << generator('T', '~', '.') << " ";
}
}
Of course the loop will never end... Are you ok with that?
Taking 3 char
s as possible tiles doesn't seem too flexible. How about making it work with arbitrary number of tiles? Something like this:
char generator(const char * tiles, int len)
{
int index = rand() % len;
return tiles[index];
}
int main()
{
srand(time(NULL));
const char * tiles = "T~.";
int len = strlen(tiles);
while(true)
{
std::cout << generator(tiles, len) << " ";
}
}
-
\$\begingroup\$ Whoops, forgot about the difference between
''
and""
when copying the code over. \$\endgroup\$Vladimir Putin– Vladimir Putin2014年08月30日 16:19:31 +00:00Commented Aug 30, 2014 at 16:19
Doing rand that way is bad.
The exact value of RAND_MAX can very but is usually 32767 (or multiples thereof).
This means rand() % 3
gives you:
0: 1/10923 // Notice that 0 and 1
1: 1/10923 // have a slightly higher probability.
2: 1/10922
There are all sorts of other issues with rand. So you may want to look at the new random libraries provided with C++11.
But if you want to use rand() in the most non biased way possible.
int getRand(int max)
{
int max = RAND_MAX/max*max; // Note integer division
// So /max*max does not cancel out.
int val;
do
{
val = rand();
} while(val >= max);
return val % max;
}
At least that will give you an even distribution.