Rolling Die!

athlon32 0 Tallied Votes 130 Views Share

Example of how to get a random number in the range of six and return a value based upon it. Please Rate/Comment/Report Bugs

#include <iostream>
#include <cstdlib>

using namespace std;

void gamefunc();

int main(void)
{
    gamefunc();
    return 0;
}

void gamefunc()
{
   int dice = (rand()%6)+1;

   /*** Now we see what you rolled ***/
   if(dice == 1)
     {
         cout << "*********\n";
         cout << "*       *\n";
         cout << "*   *   *\n";
         cout << "*       *\n";
         cout << "*********\n";

     }

        if(dice == 2)
         {
         cout << "*********\n";
         cout << "* *     *\n";
         cout << "*       *\n";
         cout << "*     * *\n";
         cout << "*********\n";

         }

      if(dice == 3)
         {
         cout << "*********\n";
         cout << "* *     *\n";
         cout << "*   *   *\n";
         cout << "*     * *\n";
         cout << "*********\n";

         }
      if(dice == 4)
         {
         cout << "*********\n";
         cout << "* *   * *\n";
         cout << "*       *\n";
         cout << "* *   * *\n";
         cout << "*********\n";

         }

      if(dice == 5)
         {
         cout << "*********\n";
         cout << "* *   * *\n";
         cout << "*   *   *\n";
         cout << "* *   * *\n";
         cout << "*********\n";

         }
      if(dice == 6)
         {
          cout << "*********\n";
          cout << "* *   * *\n";
          cout << "* *   * *\n";
          cout << "* *   * *\n";
          cout << "*********\n";
         }
}
wildgoose 420 Practically a Posting Shark

Simplify.

Use a case statement for handling each side of the die.
if you insist on if-then, then use if-elseif-else-

Shouldn't you return the value you rolled?

Or why go through all that pain. Use a table!

char *Dice[] =
{
     "*********\n" \
     "*       *\n" \
     "*   *   *\n" \
     "*       *\n"  \
     "*********\n",

    "*********\n" \
    "* *     *\n" \
    "*       *\n" \
    "*     * *\n" \
    "*********\n",

    "*********\n" \
    "* *     *\n" \
    "*   *   *\n" \
    "*     * *\n" \
    "*********\n",

   "*********\n" \
   "* *   * *\n" \
   "*       *\n" \
   "* *   * *\n" \
   "*********\n",

   "*********\n" \
   "* *   * *\n" \
   "*   *   *\n" \
   "* *   * *\n" \
   "*********\n",

   "*********\n" \
   "* *   * *\n" \
   "* *   * *\n" \
   "* *   * *\n" \
   "*********\n"
};

  n = rnd() % 6;

  cout << Dice[ n ];
athlon32 56 Junior Poster in Training

Thanks for the suggestions :) Never thought about it that way :)

jephthah 1,888 Posting Maven

your random dice aren't random.

necrolin 94 Posting Whiz in Training

Correct me if I'm wrong, but wouldn't this be pseudorandom and return the exact same series every time it's run? The way I was taught was to use a seed like this....

#include <ctime>

int seed = static_cast<int>(time(0));
srand(seed);

int randomNumber = minimum + rand() % (maximum - minimum + 1);

MosaicFuneral 812 Nearly a Posting Virtuoso

necrolin it's just:

srand(time(NULL));/*only once*/
  int var = rand()%max_requested_value;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.