0

I'm trying to make a mastermind game. The user has to guess the randomly generated colour. I wrote the random method but it keeps starting with the same element, and the next 3 are all the same but different from the first element (e.g I,o,o,o I,r,r,r). How can I fix my code?

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

#define cLength 6

int gen_rand();  
// start of main
int main() {
  int i, rNum;

  // this is the array for the easy mode
  char colours[cLength + 1] = {'r', 'o', 'y', 'g', 'b', 'i', 'v'};
  // this is the randomly generated array
  char rand[3 + 1] = "";
  // this is the guess array that the user will populate
  char guess[3 + 1] = "";

  // for statement to populate the rand array with random elements in the colours array
  rand[0] = colours[gen_rand()]; 
  rand[1] = colours[gen_rand()]; 
  rand[2] = colours[gen_rand()]; 
  rand[3] = colours[gen_rand()]; 

  printf("\n%c", rand[0]);
  printf("\n%c", rand[1]);
  printf("\n%c", rand[2]);
  printf("\n%c", rand[3]);
  // for statement to populate the guess array
  for (i = 0; i < 4; i++) {
    printf("\n Please enter a colour e.g r for Red : ");
    scanf("%c", &guess[i]);
    fflush(stdin);
  }

  printf("%c", guess[2]);
  printf("\n\n\n");
  system("pause");
}

int gen_rand() { // returns random number in range of 0 to 99
  int r = rand() % 6;
  srand(time(NULL));
  return r;
}
fenceop
  • 1,439
  • 3
  • 18
  • 29
Plisken
  • 423
  • 1
  • 4
  • 20

4 Answers4

3

Your call to srand() is mis-placed, it needs to go before you use the random number generator, in order to affect it.

Also, don't use modulo to compute the random number, it's a bad and broken method which will typically guarantee that they're not evenly distributed.

See this question for a bunch of suggestions on how to generate more high-quality random integers.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • I think float conversion probably works most of the time, but depending on your range and domain you could wind up with "holes" or "clumps" which would mess up the distribution also (maybe not only using 16-bit `rand()`), I think it is just safer to discard numbers out of range... even if it means that you average 50 calls to `rand()` for every one you use.. – Grady Player Dec 10 '14 at 14:43
  • could you share an example? – Plisken Dec 10 '14 at 15:01
  • 1
    @GradyPlayer Yeah, I backed down a bit and provided a link instead. – unwind Dec 10 '14 at 15:16
  • Choosing an item with %7 here is just fine. You're not running a simulation of billions of hands where a small statistical deviation would be measurable, so don't sweat that. But you do need to move srand(). – Lee Daniel Crocker Dec 10 '14 at 17:10
2

Call srand once. Every time your gen_rand function is called,srand will be called,but you just need to seed it just once. So move it to the start of main.


 rand[0] = colours[gen_rand()]; 
 rand[1] = colours[gen_rand()]; 
 rand[2] = colours[gen_rand()]; 
 rand[3] = colours[gen_rand()]; 

And

printf("\n%c", rand[0]);
printf("\n%c", rand[1]);
printf("\n%c", rand[2]);
printf("\n%c", rand[3]);

Can be shortened using a loop just like

for(i=0; i<4; i++)

Which you have in your code.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

You must call srand() before calling rand(), if your intention is to seed the pseudo random number generator before using rand().

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

Keep in mind that random functions uses mathematical functions du give an illusion of randomness. Most of them gives out the same number sequences if initialized with the same "seed". So remember to take as a seed a changing value, like milliseconds or something like that.

Since this is most likely a project to learn C, you can try to code your own random method (not easy!!!!), don't forget that you can can always look at the actual code of the C standard functions (^^) for inspiration. It's a great way to learn from "somewhat good" code.

(just a general input, I know. Useful nonetheless..?)

Tchou
  • 60
  • 6