You are using a version without Ads of this website. Please, consider donating:

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.

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.

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include 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:"<0){ srand(time(0)); int rndNumM = rand()%4 + 1; rndNumM=rndNumM - block + mattack; health=health-rndNumM; cout<<"Health: "<
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...
 ``12345678910111213141516171819`` ``````#include #include //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:
 ``123456789`` ``````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?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` ``````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: "<0 and health>0) { int mhit=rand()%3+mattvar+1; int hit=rand()%3+attvar+1; cout<<"Your health: "<

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)
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147`` ``````#include #include #include #include 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: "<0 and health>0) { int mhit=rand()%3+mattvar+1; int hit=rand()%3+attvar+1; cout<<"Your health: "<0 and health>0) { int mhit=rand()%3+mattvar+1; int hit=rand()%3+attvar+1; cout<<"Your health: "<
Last edited on
(part 2)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149`` ``````string whatdoyouwant; int main() { srand(time(0)); int x = 1; while(x==1){ cout<<"Sam's Adventure v.8"<>whatdoyouwant; if(whatdoyouwant == "start") { cout<<"loading -"; Sleep(1000); cout<<"--"; Sleep(1000); cout<<"---"; Sleep(1000); cout<<"---"; Sleep(1000); cout<<"---"<>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"<>whatdoyouwant; if(whatdoyouwant == "attack"){ attack = 2; x12++; }else if(whatdoyouwant == "block"){ block = 1; x12++; }else{ cout<<"no returning to top of this loop"<>whatdoyouwant; if(whatdoyouwant == "attack"){ attack=1; x12++; }else if(whatdoyouwant == "block"){ block = 2; x12++; }else{ cout<<"lolnope! returning to top of loop"<1){ switch(exp){ case 10: cout<<"congrats you leveled up!\n"<
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.

You are using a version without Ads of this website. Please, consider donating: