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.
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?
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?
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.
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;
}
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?
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.
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++;
}elseif(whatdoyouwant == "block"){
block = 1;
x12++;
}else{
cout<<"no returning to top of this loop"<<endl;
}
}
thislooop++;
}elseif(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++;
}elseif(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;
}elseif(whatdoyouwant == "help")
{
cout<<"you want help? too bad!"<<endl;
}elseif(whatdoyouwant == "quit")
{
cout<<"you can't quit bitch!"<<endl;
x=2;
}elseif(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.