Can you test a randomly generated number?

I was wondering if I created a random number generator if I could test what numbers come out. If so, can you explain the logic without giving code?


thanks, Turtley

p.s hello everybody! first post and excited to learn here!
I'm not sure what you are asking. What do you mean by "test what numbers come out"?
If you mean, see how many times a number comes out...

-Create an int to save the random number. i.e. int rndNum;

-Before the loop( next step ), create an array[ 10 ], 0 - 9. Initialize to 0;

-Create a loop for the random number to be generated in.

-Generate 100 numbers. 1 - 10.

-each time a number is generated, save to rndNum. rndNum = ( random number code here ).

-Then save the random number to the corresponding array index. array[ rndNum - 1 ]++.
-1 because a random number of 10 would be stored in array index 9.
Increment the number in this index.

-Create a loop at the end to cout all the index's of the array.

Something like that? lol.
Last edited on
I'm pretty new c++ so it may be something so basic, but i I mean is with rand(srand being used with ctime) a number will come out if I do


cout<<rand()%6+1<<endl;

I want to test what number is being printed for instance it might be 4 and I want to test if its less than three is that possible?

also if it helps, I am to create a text based game that randomly chooses an enemy to fight and damage that's done to you. would I use a RNG for this or something else?

thanks again, turtley
Last edited on
Change the cin to cout and you are in business.

cout outputs to a standard output device (monitor, printer, etc) and uses the <<

cin inputs through a standard input device (keyboard, tablet, etc) and uses the >>
that was just a mental mistake no one was supposed to see....
rndNum = rand() % 6 + 1;

That will save the random number to a variable, to which you can do some comparison on. i.e. if statement.

if random number is less than 4 - Do something
else - Do something else


And yes, I would use random numbers to pick an enemy and also, use random numbers generated within a range for the damage.

Make sure you seed it with srand(). As it turns out, computer generated random numbers aren't very random at all so you will definitely want to use the current time as your seed.
Lynx876, thank you exactly what i was looking for. I was also thinking of adding certain skills the player can choose like block and attack to manipulate the range of the generated number would this be efficient, and work?


also should I ask these questions outside of this post because my problem was solved? or can i use this for a few more things as long as it's relative?

thanks again, turtley
You could have a randomly generated number for block, then do something like:

userHP -= ( EnemyAttack - userBlock )
users HP equals users HP minus ( enemies attack minus the users block )
Work out the brackets first.


This would lessen the attack. As for efficiency, I don't really see any problems with this approach.

Ask in here, it's still relevant to your initial post (:
Last edited on
ok so basically i'm going to ask any questions here that has to do with my current project. right now i'm working on attacking(I started on a clean slate so I don't mess any thing up on my main project.) I'm trying to make the monster attack the user until its health is less than zero, but the number that is generated is the same. is there anyway to fix this?

p.s please bear with me i'm probably just being really stupid.



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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int block = 1;
int attack = 2;
int health =10 + block*2;

int mhealth = 10;
int mattack =1;
int mblock =1;


int main()
{

cout<<" Health:"<<health<<endl;
cout<<"\n           Monster health:"<<mhealth<<endl;

while(health>0){
            srand(time(0));
 int   rndNumM = rand()%4 + 1;

rndNumM=rndNumM - block + mattack;
health=health-rndNumM;
cout<<"Health: "<<health<<endl;
cout<<"damge taken: "<<rndNumM<<endl;
}


}
Move line 22, outside of the while loop. (:


Edit:
Just write a function for it! You'll need to get random numbers more than once. So...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <time.h>
#include <iostream>

//Function prototypes.
int randomRange( int _Min, int _Max );

int main()
{
	srand( unsigned( time( NULL ) ) );

	for( int i = 0; i < 10; ++i )
	{
		int rndNum = randomRange( 7, 23 );    

		std::cout << rndNum << '\n';
	}

	return 0;
}


Random range function:
1
2
3
4
5
6
7
8
9
int randomRange( int _Min, int _Max )
{
	//add 1 to the max, so it's included in the result.
	++_Max;

	int x = _Min + rand() % ( _Max - _Min );

	return x;
}


Here's the code running:
http://liveworkspace.org/code/1C3W4T$0
Last edited on
Use chi-square testing. What language are you using? I can offer a C++ example. Basically

Place random numbers in buckets (many times).
The number of buckets minus one is the degrees of freedom.
Compare the bucket tallies against "expected" tallies, yielding a chi-square result.
Use a chi-square calculator to see the probability of getting those results.
ok thank you to everybody that has helped me so far. I have two more things I want to add to my program before I consider it done. a level system and fixing the hit range.(also when I learn more c++ a saving system)

what I mean by hit range is that for every two levels of attack you have you can the chance to hit one higher. for instance if I have one attack I can hit from 1-3 if I have two I would be able to hit 1-4. here is a example with one of my monsters, is the equation I have for it right? I can't seem to get it.

p.s if you want to yell at me about how unorganized my code is, please do, I want to learn and improve! lastly on a scale from one to ten how difficult would you think a save system would be?

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
double attvar = attack/2;
double mattvar = matt/2;


//monsters

void doGoblin(){
    int mhealth = 7;
    int mdef = 1;
    int matt = 2;


    while(mhealth>0 and health>0)
    {
    int mhit=rand()%3+mattvar+1;
    int hit=rand()%3+attvar+1;

        cout<<"Your health: "<<health<<endl;
        cout<<"Goblin health: "<<mhealth<<"\n\n"<<endl;
        Sleep(500);
        health = health - mhit;
        mhealth = mhealth - hit;
        cout<<"Damge delt: "<<hit<<endl;
        cout<<"Damgae taken: "<<mhit<<"\n\n"<<endl;
        if(mhealth <=0)
        {
            cout<<"Goblin health: "<<mhealth<<endl;
            cout<<"Your health: "<<health<<endl;
            cout<<"You won!"<<endl;
            exp = exp+1;
            cout<<"\n you gained 1 exp for a grand total of "<<exp<<" experience!"<<endl;


        }else if(health <=0)
        {
        cout<<"Goblin health: "<<mhealth<<endl;
        cout<<"Your health: "<<health<<endl;
        cout<<"You lost!"<<endl;
        }else if(health<=0 and mhealth<=0)
        {
            cout<<"its a tie!"<<endl;
        }else{}

    }
}
void doTroll()
{

    int mhealth = 15;
    int mdef = 3;
    int tatt = 2;

        while(mhealth>0 and health>0)
    {
    int mhit=rand()%3+mattvar+1;
    int hit=rand()%3+attvar+1;

        cout<<"Your health: "<<health<<endl;
        cout<<"Troll health: "<<mhealth<<"\n\n"<<endl;
        Sleep(500);
        health = health - mhit;
        mhealth = mhealth - hit;
        cout<<"Damge delt: "<<hit<<endl;
        cout<<"Damgae taken: "<<mhit<<"\n\n"<<endl;
        if(mhealth <=0)
        {
            cout<<"Troll health: "<<mhealth<<endl;
            cout<<"Your health: "<<health<<endl;
            cout<<"You won!"<<endl;
            exp = exp+5;
            cout<<"\n you gained 5 exp for a grand total of "<<exp<<" experience!"<<endl;


        }else if(health <=0)
        {
        cout<<"Troll health: "<<mhealth<<endl;
        cout<<"Your health: "<<health<<endl;
        cout<<"You lost!"<<endl;
        }else if(health<=0 and mhealth<=0)
        {
            cout<<"its a tie!"<<endl;
        }else{}

    }
}



thanks, turtley.
A save system shouldn't be too hard once you understand how to organize it. Basically you have to work on both ends at the same time, your save function has to put variables into a file in a way that your load function will be able to call it back and use it.

Right now would be a good time to start learning ifstream and ofstream since your game is just starting and it doesn't have too many variables yet.

So what I mean by "working at both ends" is that when you push a variable into a file it only stores the value that was inside that variable, not the name of the variable itself, so if you save a character by pushing the variables into a file as file<<character<<attack<<defense<<exp; then make sure that your load function pulls those values into the right variables by using the same order. file>>character>>attack>>defense>>exp;

It would be a good idea to start your save/load functions now so you can add important variables to your functions as they come up.
Ok thanks, newbieg that helped a lot. btw that is a a little bit of my code. if anyone wants to help me improve my codeing by telling me how to do things better here it is.


(part 1)
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <windows.h>
using namespace std;

int exp = 0;

int level = 1;

double attack, matt, mdef;

double block = block - matt;

double health = 10+block*2;

double mblock =-attack;
double mhealth =+mdef*2;

int monsterid = rand()%3+1;

double attvar = attack/2;
double mattvar = matt/2;


//monsters

void doGoblin(){
    int mhealth = 7;
    int mdef = 1;
    int matt = 2;


    while(mhealth>0 and health>0)
    {
    int mhit=rand()%3+mattvar+1;
    int hit=rand()%3+attvar+1;

        cout<<"Your health: "<<health<<endl;
        cout<<"Goblin health: "<<mhealth<<"\n\n"<<endl;
        Sleep(500);
        health = health - mhit;
        mhealth = mhealth - hit;
        cout<<"Damge delt: "<<hit<<endl;
        cout<<"Damgae taken: "<<mhit<<"\n\n"<<endl;
        if(mhealth <=0)
        {
            cout<<"Goblin health: "<<mhealth<<endl;
            cout<<"Your health: "<<health<<endl;
            cout<<"You won!"<<endl;
            exp = exp+1;
            cout<<"\n you gained 1 exp for a grand total of "<<exp<<" experience!"<<endl;


        }else if(health <=0)
        {
        cout<<"Goblin health: "<<mhealth<<endl;
        cout<<"Your health: "<<health<<endl;
        cout<<"You lost!"<<endl;
        }else if(health<=0 and mhealth<=0)
        {
            cout<<"its a tie!"<<endl;
        }else{}

    }
}
void doTroll()
{

    int mhealth = 15;
    int mdef = 3;
    int tatt = 2;

        while(mhealth>0 and health>0)
    {
    int mhit=rand()%3+mattvar+1;
    int hit=rand()%3+attvar+1;

        cout<<"Your health: "<<health<<endl;
        cout<<"Troll health: "<<mhealth<<"\n\n"<<endl;
        Sleep(500);
        health = health - mhit;
        mhealth = mhealth - hit;
        cout<<"Damge delt: "<<hit<<endl;
        cout<<"Damgae taken: "<<mhit<<"\n\n"<<endl;
        if(mhealth <=0)
        {
            cout<<"Troll health: "<<mhealth<<endl;
            cout<<"Your health: "<<health<<endl;
            cout<<"You won!"<<endl;
            exp = exp+5;
            cout<<"\n you gained 5 exp for a grand total of "<<exp<<" experience!"<<endl;


        }else if(health <=0)
        {
        cout<<"Troll health: "<<mhealth<<endl;
        cout<<"Your health: "<<health<<endl;
        cout<<"You lost!"<<endl;
        }else if(health<=0 and mhealth<=0)
        {
            cout<<"its a tie!"<<endl;
        }else{}

    }
}
void doDragon()
{

    int mhealth = 20;
    int mdef = 10;
    int matt = 10;

        while(mhealth>0 and health>0)
    {
    int mhit=rand()%3+mattvar+1;
    int hit=rand()%3+attvar+1;

        cout<<"Your health: "<<health<<endl;
        cout<<"Dragon health: "<<mhealth<<"\n\n"<<endl;
        Sleep(500);
        health = health - mhit;
        mhealth = mhealth - hit;
        cout<<"Damge delt: "<<hit<<endl;
        cout<<"Damgae taken: "<<mhit<<"\n\n"<<endl;
        if(mhealth <=0)
        {
            cout<<"Dragon health: "<<mhealth<<endl;
            cout<<"Your health: "<<health<<endl;
            cout<<"You won!"<<endl;
            exp = exp+10;
            cout<<"\n you gained 10 exp for a grand total of "<<exp<<" experience!"<<endl;


        }else if(health <=0)
        {
        cout<<"Dragon health: "<<mhealth<<endl;
        cout<<"Your health: "<<health<<endl;
        cout<<"You lost!"<<endl;
        }else if(health<=0 and mhealth<=0)
        {
            cout<<"its a tie!"<<endl;
        }else{}

    }

}
Last edited on
(part 2)

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
string whatdoyouwant;

int main()
{
srand(time(0));

    int x = 1;
while(x==1){
   cout<<"Sam's Adventure v.8"<<endl;

    cout<<"Start"<<endl;
    cout<<"Help"<<endl;
    cout<<"Quit"<<endl;

        cin>>whatdoyouwant;

    if(whatdoyouwant == "start")
    {
        cout<<"loading -";
        Sleep(1000);
            cout<<"--";
                Sleep(1000);
                    cout<<"---";
                    Sleep(1000);
                        cout<<"---";
                        Sleep(1000);
                            cout<<"---"<<endl;
                            cout<<"finished\n\n"<<endl;


                            int attack =0;
                            int block =0;
                        int thislooop=521;
                        while(thislooop==521){

                                cout<<"which stat do you want a point in? (you have two points.)\n\n"<<"Attack = 0\n"<<"Block = 0"<<endl;

                                    cin>>whatdoyouwant;

                                    if(whatdoyouwant== "attack"){
                                        attack=1;

                                        int x12 =10;
                                            while(x12 == 10){
                                                        cout<<"which stat woud you like to put your second point into?\n\n"<<"Attack = 1\n"<<"Block = 0\n"<<endl;
                                                                cin>>whatdoyouwant;

                                                                        if(whatdoyouwant == "attack"){
                                                                            attack = 2;
                                                                            x12++;

                                                                        }else if(whatdoyouwant == "block"){
                                                                                    block = 1;

                                                                                    x12++;
                                                                        }else{
                                                                        cout<<"no returning to top of this loop"<<endl;
                                                                            }
                                                                        }
                                                thislooop++;
                                    }else if(whatdoyouwant == "block"){
                                        block=1;
                                    int x12=10;
                                        while(x12 == 10){

                                            cout<<"Which stat would you like to put your second point into?\n\n"<<"Attack = 0\n"<<"Block = 1\n"<<endl;
                                                cin>>whatdoyouwant;

                                                        if(whatdoyouwant == "attack"){
                                                            attack=1;

                                                                                        x12++;
                                                        }else if(whatdoyouwant == "block"){
                                                            block = 2;

                                                                    x12++;
                                                        }else{
                                                        cout<<"lolnope! returning to top of loop"<<endl;
                                                        }
                                        }

                                        thislooop++;
                                    }else{
                                    cout<<"lolnope! returning to choose..."<<endl;
                                    }

                            cout<<"Your stats are:\n"<<"Attack:"<<attack<<"\nBlock: "<<block<<endl;
                        }
                        monsterid = rand()%3+1;
                switch(monsterid)
                {
                case 1:
                    doGoblin();
                   break;
                case 2:
                    doTroll();
                   break;
                case 3 :
                    doDragon();
                    break;
            }
    if(exp>1){
            switch(exp){
            case 10:
                cout<<"congrats you leveled up!\n"<<endl;
                    health =15;
                cout<<"Your health has increased by 5 points"<<endl;
                cout<<"You also gained another 1 more skill point!\n What you you liek to put it into?\n\n Attack: "<<attack<<"Block: "<<block<<endl;

                break;

            default: //do nothing
                cout<<""<<endl;

            }
            }
x=2;

    }else if(whatdoyouwant == "help")
    {
    cout<<"you want help? too bad!"<<endl;
    }else if(whatdoyouwant == "quit")
    {
        cout<<"you can't quit bitch!"<<endl;
            x=2;
    }else if(whatdoyouwant == "backdoor"){
        attack =2;
        block = 11;
                 monsterid = rand()%3+1;
                switch(monsterid)
                {
                case 1:
                    doGoblin();
                   break;
                case 2:
                    doTroll();
                   break;
                case 3 :
                    doDragon();
                    break;
                }
            x=2;
    }else{cout<<"lolnope! returning to top"<<endl;

    }

}

}
It will probably be a good idea to start learning classes and objects, right now you are having to make a function for each enemy and you're making a battle system for each one too. Classes will let you just make one battle system and then plug in the variables for each enemy from a class. Classes will also save you time since you can write a single function in a class and have it apply to any character.

Hmm... Maybe start with the save/load program, that's kind of important to get started in as a game programmer, but be ready to learn classes, they'll save you several thousand lines of code at this rate.
Topic archived. No new replies allowed.