Thread: Homework for Random Generator

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    11

    Post Homework for Random Generator

    For homework I am tasked with making a random generator that gives a different outcome whenever it is executed using function prototypes, switches, and enum. I tried everything I could, but the outcomes I got was after executing the program the result would just repeat itself a few times before going to a different result it even would bring me back to bash prompt.

    What am I doing wrong?

    I compiled it using GCC if that helps.

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    enum meme {YEE, WAE, VERY, HARAMBE};
    
    
    int dankestmemes(void); // function prototype
    
    int main(void) {   
    
    srand(time(NULL)); // randomize random generator using current time
    //int randomNumber = rand() % 5;    
        int memes;
        enum meme dankMeme; // can contain YEE, WAE, VERY, HARAMBE
        int dank = dankestmemes(); // When program first executes
        
        switch((dank + randomNumber) % 5){  //puts you in a meme tribe
            
            case 1: 
            dankMeme = YEE;  
            printf("Welcome to the tribe of the Yee Dinosaur. "); 
            printf("You are very wise for choosing this tribe\n"); 
            break;        
                
            case 2: 
            dankMeme = WAE;
            printf("You have found da wae to da Ugandan Knuckle ");
            printf(" Tribe my brutha. \n");
            printf("Now let us look for our queen.\n"); 
            break;
            
            case 3: 
            dankMeme = VERY;
            printf("MUCH WOW. VERY AMAZE YOU JOIN DOGE TRIBE!\n");
            break;
            
            case 4: 
            dankMeme = HARAMBE;
            printf("You have been blessed with the precense of Harambe\n ");
            printf("in the Harambe Tribe, here we honor the greatest thing \n");
            printf("that sacrificed it's life trying to protect a child. \n");
            printf("Harambe is gone, but will never be forgotten!\n");
            break;
             }
        }
    int dankestmemes(void){
    
     return (rand() % 6) + (rand() % 6) + 2;  // Randomizes outcome
     }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Modulus Operator in C and C++ - Programming Tutorials - Cprogramming.com

    You need to understand what modulus operator does.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    11
    Quote Originally Posted by stahta01 View Post
    Modulus Operator in C and C++ - Programming Tutorials - Cprogramming.com

    You need to understand what modulus operator does.

    Tim S.
    I understand what it does, did I not put it in correctly though?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Modulus 5 results in a value from 0 to 4! You do not handle the value of 0 in your switch statement.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need a random number generator thats not compleatly random
    By thedodgeruk in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2011, 06:48 AM
  2. Random Generator
    By jpjpolski in forum C Programming
    Replies: 4
    Last Post: 05-29-2011, 12:56 AM
  3. Random generator 0 1
    By Dedalus in forum C Programming
    Replies: 3
    Last Post: 06-30-2009, 08:44 AM
  4. looking for a random # generator
    By w/ possion? in forum C Programming
    Replies: 4
    Last Post: 02-28-2003, 07:58 AM
  5. Random Name Generator
    By jdinger in forum C++ Programming
    Replies: 6
    Last Post: 04-11-2002, 11:36 AM

Tags for this Thread