Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Shit in C

Name: Anonymous 2009-02-25 10:09

I'm trying to set the contents of a string based on what random number is generated by my srand.

dice = rand() % 10 + 1;

if ( dice = 1 )
word[20] = "Success";

^-- this shit doesn't work, though.

tl;dr. I want my "dice rolls" to generate a random number, random number corresponds to a pre-set word, and the preset word to be printed.

I'm probably going about it the wrong way. Suggestions from experts?

Name: Anonymous 2011-12-30 9:39

Here is how I would do it.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/* words[0] indicates failure
   words[1] indicates success */
char * words [] = {
  "failure",
  "success"
};

int main (void) {
  int i;
  int dice;

  srand(time(NULL));

  for (i = 0; i < 10; i++) {
    dice = rand() % 10 + 1;
    printf("%s: you rolled %d\n",
       words[dice == 1], dice);
  }

  return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List