Why does this always return same number?

Pages: 12
I've wrote a small function and included random number generator but it always returns the same number... WHY???

color = rand() % 4 + 1;

Are you calling srand()?
There's no way (as far as I know) to create a REAL random number in C++. However... You can try this:

1
2
int seed = static_cast<int>(time(0));
srand(seed);


This will create a random number based off of your computer clock. It's not a REAL random number, but it won't produce the same number every time.
try
color = 1 + rand() % 4;

it will take a random number, divide it by 4 displaying the remainder which will be 0-3.. add the +1 at the beginning that way it will 1-4, at least what I am understanding from my book anyhow.
You can do all that static_cast crap, or you can do this:
srand(time(0));
Some prefer to use NULL instead of zero. I like zero.
of course i used srand...
i still can't produce random number by this...
it's similair to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    int color;
    srand ( time(NULL) );
    color = rand() % 4 + 1; 
    cout << color;
    return 0;
   }

closed account (z05DSL3A)
So what do you get as ouput with code like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    int color;
    srand ( (unsigned) time(NULL) );
    for(int i= 0;i < 50; i++)
    {
        color = rand() % 4 + 1; 
        cout << color <<endl;
    }
    return 0;
}
@ Grey Wolf
It works but i still don't understand how to use it...
I'm trying to make something like random card generator for a Blackjack program I'm trying to make xD...

It looks like this so if you could help I would really appreciate your help...
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
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    //INITIALIZE
    int color,value;
    string value_str,color_str;
    srand ( (unsigned) time(NULL) );

    
    //RANDOM COLOR
    color = rand() % 4 + 1; 
    if (color = 1)
    color_str = "Heart";
    else if (color = 2)
    color_str = "Diamond";
    else if (color = 3)
    color_str = "Club";
    else
    color_str = "Spade";
    
    //RANDOM VALUE
    value = rand() % 13 + 1;
    if (value=1)
    value_str = "Ace";
    else if (value = 2)
    value_str = "2";
    else if (value = 3)          
    value_str = "3";          
    else if (value = 4)
    value_str = "4";
    else if (value = 5)
    value_str = "5";
    else if (value = 6)
    value_str = "6";
    else if (value = 7)
    value_str = "7";
    else if (value = 8)
    value_str = "8";
    else if (value = 9)
    value_str = "9";
    else if (value = 10)
    value_str = "10";
    else if (value = 11)
    value_str = "Jack";
    else if (value = 12)
    value_str = "Queen";
    else
    value_str = "King";
    
    //DISPLAY
    cout << color_str << " " << value_str << endl;
        
    //END           
    system("PAUSE");
    return EXIT_SUCCESS;
}
All the if conditions are wrong, you should use the == operator ( if (value==1) )

You can use a switch instead of many if-else
Last edited on
OMG I can't believe I didn't put double ==...
*ashamed*
Ty man =)...
P.S.Since you can see I am total noob could you possibly explain me how to use switch?
switch works for integral types as follows:
1
2
3
4
5
6
7
8
9
10
11
12
switch( Variable )
{
    case 1:
        //Variable == 1
        break;//as you have to put break you don't need braces
    case 2: //if the break is not found it will perform the next case
    case 3:
       //Variable == 2 || Variable == 3
       break;
    default:
       //All other values
}




http://www.cplusplus.com/doc/tutorial/control.html#switch
Last edited on
OK ty I get it now...
I made FINALLY that random card generator and I made a "Hit" like in BJ...
The problem is that i don't know where to define points = 0 because if I don't points get messed up and if I define them in void they always get restarted but if I define points value in main function, void doesn't recognize it... HELP!!!!
Btw, this is still in If-Else form, I'll fix it afterward I'm too lazy to do it now =P
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

void Random ();

int main(int argc, char *argv[])
{   
    Random ();           
    system("PAUSE");
    return EXIT_SUCCESS;
}


void
Random ()
{
    //INITIALIZE
    int color,value,points;
    string value_str,color_str,t;
    srand (time(0));

    
   
    //RANDOM COLOR
    color = rand() % 4 + 1; 
    if (color == 1) 
    color_str = "Heart";
    else if (color == 2)
    color_str = "Diamond";
    else if (color == 3)
    color_str = "Club";
    else
    color_str = "Spade";
    
    //RANDOM VALUE
    value = rand() % 13 + 1;
    if (value == 1)
    {
    value_str = "Ace";
    points = points + 11;
}
    else if (value == 2)
    {
    value_str = "2";
    points = points + 2;
}
    else if (value == 3)          
    {
    value_str = "3"; 
    points = points + 3;
}         
    else if (value == 4)
    {
    value_str = "4";
    points = points + 4;
}
    else if (value == 5)
    {
    value_str = "5";
    points = points + 5;
}
    else if (value == 6)
    {
    value_str = "6";
    points = points + 6;
}
    else if (value == 7)
    {
    value_str = "7";
    points = points + 7;
}
    else if (value == 8)
    {
    value_str = "8";
    points = points + 8;
}
    else if (value == 9)
    {
    value_str = "9";
    points = points + 9;
}
    else if (value == 10)
    {
    value_str = "10";
    points = points + 10;
}
    else if (value == 11)
    {
    value_str = "Jack";
     points = points + 10;
}
    else if (value == 12)
    {
    value_str = "Queen";
     points = points + 10;
}
    else
    {
    value_str = "King";
     points = points + 10;
}

    
        
    //DISPLAY
    cout << color_str << " " << value_str << endl;

       
    //HIT OR STAY
    cout << "Points :" << points << endl;
    cout << "Hit (t) or Stay (s)?" << endl;
    cin >> t;
    if (t == "t")
    Random ();
}


    
        
Define any variable you want accessible to the whole program outside and before the main function.

i.e.
#include <iostream>
int somevariable = 0;

int main()
ok tnx and how can i make this end here???

1
2
3
4
5
6
7
8
if (points <= 21)
    cout << color_str << " " << value_str << endl;
    else
    {
    cout << "Points :" << points << endl;
    cout << "Looser!" << endl;
    ?????????????????????????????
    }
Last edited on
return 0?
Don't you think I already tried that?
Compiler says : "return statement with a value,in function returning void"...
return; without any value
weeeeeee tnx m8 =))
I think I've done most of it I would just appreciate if someone could explain me how to use switches instead of else-ifs and how to make cards not so repetitive and also, the most important, HOW TO DISABLE MAKING SAME CARDS MULTIPLE TIMES???
i.e.
Spade 2
Spade 2
Club 2
Heart 5
Spade 2
etc...

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

void Random (), Random_PC ();
int points=0;


int main(int argc, char *argv[])
{   
    Random ();           
    system("PAUSE");
    return EXIT_SUCCESS;
         
}


void
Random ()
{
    //INITIALIZE
    int color,value,PC_points;
    string value_str,color_str,t;
    srand((unsigned)time(0) );

       
    //RANDOM COLOR
    color = rand() % 4 + 1; 
   
    if (color == 1) 
    color_str = "Heart";
    else if (color == 2)
    color_str = "Diamond";
    else if (color == 3)
    color_str = "Club";
    else
    color_str = "Spade";
   
   
     
    //RANDOM VALUE
    value = rand() % 13 + 1;
    if (value == 1)
    {
    value_str = "Ace";
    points = points + 11;
}
    else if (value == 2)
    {
    value_str = "2";
    points = points + 2;
}
    else if (value == 3)          
    {
    value_str = "3"; 
    points = points + 3;
}         
    else if (value == 4)
    {
    value_str = "4";
    points = points + 4;
}
    else if (value == 5)
    {
    value_str = "5";
    points = points + 5;
}
    else if (value == 6)
    {
    value_str = "6";
    points = points + 6;
}
    else if (value == 7)
    {
    value_str = "7";
    points = points + 7;
}
    else if (value == 8)
    {
    value_str = "8";
    points = points + 8;
}
    else if (value == 9)
    {
    value_str = "9";
    points = points + 9;
}
    else if (value == 10)
    {
    value_str = "10";
    points = points + 10;
}
    else if (value == 11)
    {
    value_str = "Jack";
     points = points + 10;
}
    else if (value == 12)
    {
    value_str = "Queen";
     points = points + 10;
}
    else
    {
    value_str = "King";
     points = points + 10;
}

    
        
    //DISPLAY
    if (points <= 21)
    cout << color_str << " " << value_str << endl;
    else
    {
    cout << "Points :" << points << endl;
    cout << "Looser!" << endl;
    return;
    }
    
    

       
    //HIT OR STAY
    cout << "Points :" << points << endl;
    cout << "Hit (t) or Stay (s)?" << endl;
    cin >> t;
    if (t == "t")
    Random ();
    if (t == "s")
    {
      again:
             PC_points = rand() % 35 + 1;
      if (PC_points < 18)
      goto again;
      cout << "PC Points: " << PC_points << endl;
      if (points < PC_points)
      cout << "Looser!" << endl;
      else if (points > PC_points)
      cout << "Winner!" << endl;
      else 
      cout << "Draw!" << endl;
      } 
}    



ty
Last edited on
Move srand in main before the Random call.

Lines 135-138 should better be
1
2
3
do
    PC_points = rand() % 35 + 1;
while(PC_points < 18);



If you want to use a switch you could do it this way:
1
2
3
4
5
6
7
8
9
10
11
12
switch(value)
{
    case 1:
        value_str = "Ace";
        points = points + 11;
        break;
    case 2:
        value_str = "2";
        points = points + 2;
        break;
    //...
}
It was ony a suggestion, if you like you can keep the if-else
Pages: 12