1

I'm learning C and find rand() very strange:

I've the following game: I have played the game several times and the correct answer was something near 47.

For example:

  • between 1 and 100 it would be 47
  • between 1 and 500 it would be 247 or 347.
  • between 1 and 1000 it would be 347.

I don't want the number to be easily guessed after the third time. Is there any problem? how can I modify this code to do the job?
#include<stdio.h>
#include<stdlib.h>
void main()
{int n,l,c,tries;


char yn;

first:
n=0,c=0,l=0,tries=0;
do{
printf(" 1: Easy\n 2:Medium\n 3:hard\n");
scanf("%d",&l);
}while(l<1 || l>3);
 switch(l)
 {
 case 1:
            n =  rand % 100 +1;
            printf("I picked a number between 1 and 100\n");
            break;
 case 2:
            n = rand()%500 + 1;
            printf("I picked a number between 1 and 500\n");
            break;
 case 3:
            n = rand()%1000 + 1;
            printf("I picked a number between 1 and 1000\n");
            break;
 }
 do
 {
 tries++;
 printf("Enter your guess: \t");
 scanf("%d",&c);
 if(c == n)
  printf("You Won\n number of gusses= %d\n", tries);
 else
 {
    if(c > n)
    printf("High\n");
    else
    printf("Low\n");
 }
 }while(c !=n);

 printf("Try Again \?(Y/N)\n");
 scanf(" %c",&yn);

 if(yn == 'Y' || yn == 'y')
 goto first;
} 
  • 4
    Use srand to seed your random number generator. – Joe Dec 12 '13 at 11:46
  • 1
    In game development the fact that randomization is not really random is an adavantage, because you don't really want to debug a complex non-deterministic game. :) – Devolus Dec 12 '13 at 11:53
  • 1
    You forgot the parentheses in your case 1. That means the outcome is the _address_ of the rand function, rather than the result of a call to it. – Mr Lister Dec 12 '13 at 12:04
  • 1
    Also, I see you're using `goto`. Don't. – Mr Lister Dec 12 '13 at 12:05
  • 1
    @MrLister is it a bad practice to use goto; what should I replace it with. –  Dec 12 '13 at 14:18
  • The same way as the other loops in your code: `do {` at the beginning, `} while (condition);` at the end. – Mr Lister Dec 12 '13 at 15:39
  • 1
    @MrLister Why is it bad to use goto. –  Dec 12 '13 at 15:45
  • 1
    Because it can break the "normal" program flow. It can jump into or out of loops, if-branches, switch statements etc. You can no longer make program flow diagrams of programs that contain goto. Also, [Why is it bad to use goto?](http://stackoverflow.com/questions/3444060/why-is-it-bad-to-use-goto), [GOTO still considered harmful?](http://stackoverflow.com/questions/46586/goto-still-considered-harmful) and the links in there. – Mr Lister Dec 12 '13 at 15:57

2 Answers2

3

If you reboot your pc you'll see that the results will be different.
Random is not clever as you expect, and, to tell you the truth, is not properly random! (:

By the way, to provide a different seed to the random alghoritm, you have to call, only one time:

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

srand(time(0));

Take a look to the documentation!

edit

To improve your code:

int randN = 100;
switch (l) {
  case 1:
    break;
  case 2: 
    randN = 500;
    break;
  case 3:
    randN=  1000;
    break;
  default:
    randN = rand() % 1000 + 1;
    break;
 }
 n = rand() % randN + 1;
 printf("I picked a number between 1 and %i.\n", randN);
Community
  • 1
  • 1
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
  • 1
    It gives me an error: **Illegal use of pointer in function main** (line 19, where 'n=rand%100+1;') –  Dec 12 '13 at 11:56
  • 1
    @Vethune only when I add srand. –  Dec 12 '13 at 14:19
  • oh that did it . I also forgot the parantheses in case 1. –  Dec 12 '13 at 15:47
  • 1
    @Velthune just a quick note: there's a typo in your `default`: breack, not break. Not a big deal but thought you should know. – Mike G Jan 29 '14 at 14:30
1

use srand and then use rand i.e

int main() 
{
    srand(time(0)); // use current time as seed for random generator
    int random_variable = rand();
    printf("Random Value= %d",random_variable);
    return 0;

}
Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
Omer Obaid
  • 416
  • 1
  • 6
  • 24