in C 'rand()' dose not seem to want to spit out the numbers i'd like it to.

Im trying to make a basic game just to test my skills. I googled a function that would generate a random number in C and it seemed to work... but when trying to call the function to generate random stats for my game it seems to fail me... It spits out numbers that are either ridiculously large or ridiculously small... or just equal to zero. Heres my code, can anyone help me with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LEVEL_PLAYER if(player[4] >= 1000){player[0] = player[0] + 1; player[4] = player[4] - 1000;}

int main()
{
    char choice[1];
    int loop;

    int player[6];
    char player_name[20];

    int enemy_1[5];
    int enemy_2[5];
    int enemy_3[5];
    int enemy_4[5];
    int boss_1[5];
    int enemy_6[5];
    int enemy_7[5];
    int enemy_8_1[5];
    int enemy_8_2[5];
    int enemy_9[5];
    int boss_2[5];

    printf("Game By: Paul Micciche\n");
    printf("Crossing the Bridge (Alpha V1.0)");

    printf("Would you like to enter your stats yourself? Or would you like them to be randomly genorated?\nA) Genorate stats randomly.\nB) Enter stats myself.\nX: ");
    scanf("%s", &choice);

     srand(time(NULL));

    if(choice == 'a' || choice == 'A'){

        player_rand_lvl:
        player[0] = (rand() %2);
        if(player[0] < 1){player[0] = 0; goto player_rand_lvl;}

        player_rand_attack:
        player[1] = (rand() % 1501);
        if(player[1] < 45){player[1] = 0; goto player_rand_attack;}

        player_rand_defense:
        player[2] = (rand() % 150);
        if(player[2] < 45){player[2] = 0; goto player_rand_defense;}

        player_rand_health:
        player[3] = (rand() % 500);
        if(player[3] < 100){player[3] = 0; goto player_rand_health;}

        player_rand_xp:
        player[4] = (rand() % 1000);
        if(player[4] < 50){player[4] = 0; goto player_rand_xp;}
    }

LEVEL_PLAYER;

printf("Level: %i\nAttack: %i\nDefense: %i\nHealth: %i\nStarting XP: %i", player[0], player[1], player[2], player[3], player[4]);

    printf("\n\nWhat is your name?...\nX: ");
    scanf("%s", &player_name);
    printf("Ahh, so your name is %s?", player_name);

    return 0;
}
What is the code supposed to do? In other words, what numbers (range of numbers) do you expect?

rand() % N will generate a (pseudo) random integer between 0 and N - 1 inclusively.
For example, rand() % 100 will generate a random integer between 0 and 99.
Last edited on
Well in the snippit of code i posted its just supposed to genorate random stats. You can see it in the first IF statement in the code
No one still watching this?
There is nothing obviously wrong with your use of rand().

You still haven't answered the question: What's wrong with the numbers you are getting?
Oh sorry. They are all far too high. Here are the numbers i always get, and they seem to be fairly constant.

level: 4201169
attack: 2293744
defense: 4201262
health: 4201168
starting XP: 3341445
But what range do you want? Between 1 and 100?
for level it should be 1 to 3. For attack it should be 45 to 150 (in the code it says 1501 which was a mistake) for defense it should be 45 to 150. for health it should be 100 to 500. and for starting xp it should be 50 to 1000.
This code should be written as follows:
1
2
3
4
//Current code
player_rand_lvl:
        player[0] = (rand() %2);
        if(player[0] < 1){player[0] = 0; goto player_rand_lvl;}


1
2
//Better code 
player[0] = rand() % 2 + 1; //Will make player[0] either 1 or 2.  


1
2
3
player_rand_attack:
        player[1] = (rand() % 1501);
        if(player[1] < 45){player[1] = 0; goto player_rand_attack;}


 
player[1] = rand() % 1501 + 45; //Will make player[1] 45-1546 


etc...

Something like this would be so much easier to make using classes though. I would highly recommend learning classes and using them for this project.
Last edited on
Haha, well I have to agree with you there. But this is really just to test what i have learned from a tutorial series I'm watching by the guys at wibit.net. They seem to be pretty set on making all there students learn C before anything else, and being someone who is fairly new I'm not really one to object. I have worked with some OO programing languages and know this would be much easier if i were to be using objects but oh well, I guess I gotta work with what I know for now and upgrade later.

And I just entered the code the way you suggested and got the same output... here is how the IF statement goes now:
1
2
3
4
5
6
7
8
9
10
11
12
    if(choice == 'a' || choice == 'A'){

        player[1] = rand() % 2 + 1;

        player[1] = rand() % 150 + 45;

        player[2] = rand() % 150 + 45;

        player[3] = rand() % 100 + 500;

        player[4] = rand() % 1000 + 100;
    }
I am still toying around with it but cant seem to figure it out...
I have updated it a bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LEVEL_PLAYER if(player[4] >= 1000){player[0] = player[0] + 1; player[4] = player[4] - 1000;}

int main()
{
    char choice[1];
    int loop;

    int player[6];
    char player_name[20];

    printf("Game By: Paul Micciche\n");
    printf("Crossing the Bridge (Alpha V1.0)\n\n");

    printf("Would you like to enter your stats yourself? Or would you like them to be randomly genorated?\nA) Genorate stats randomly.\nB) Enter stats myself.\nX: ");
    scanf("%s", &choice);

     srand(time(NULL));

    if(choice == 'a' || choice == 'A'){

        player[0] = rand() % 2 + 1;

        player[1] = rand() % 150 + 45;

        player[2] = rand() % 150 + 45;

        player[3] = rand() % 100 + 500;

        player[4] = rand() % 1000 + 100;
    }

LEVEL_PLAYER;

printf("Level: %i\nAttack: %i\nDefense: %i\nHealth: %i\nStarting XP: %i", player[0], player[1], player[2], player[3], player[4]);

    printf("\n\nWhat is your name?...\nX: ");
    scanf("%s", &player_name);
    printf("Ahh, so your name is %s?", player_name);

    return 0;
I just noticed your #define has a typo at the end. Can the existence of this #define at all be explained? Are you random numbers now working as expected?
I dont know (and dont think) the #define would do it... i could be wrong though. And yes my random numbers are not working as expected.
Nope, tested it. THe #define did not make a difference.
You are comparing a pointer to a char in line 22, try doing this:
1
2
3
4
// Two different ways of doing it (ONLY CHOOSE ONE)
if (*choice == 'a' || choice[0] == 'A') {
    // etc.
}


This means that they are just being associated junk that happened to exist when they were declared. Why are you using a char array of size 1 anyway? Just use a single char and tell scanf that.

EDIT:
Also, you are using scanf incorrectly anyway, because you are supplying an array of at least 2 to an array of size 1 (using %s adds a null terminator to the end of the string). Use %c instead.
Last edited on
Ha ha! Nice catch about that if.

Usually people declare such things as a straight-up char, not an array of 1. So I totally missed that.

Yep, that would have done it...
Thank you very much! That seems to have worked greatly! Haha and thanks for the advice on scanf as well... Still kinda new to this, so i need all the advice i can get.
Topic archived. No new replies allowed.