need random numbers or a pause

I'm trying to make a game, and i;m having issues trying to get truly random number's everytime i use the function. i use rand() and srand(time(NULL)) functions, but when i get to the "attack" i try it but it will give me the same value when i hit it, but only when i hit it fast in sequence(basic attack just testing the randomness of it). I believe that it does this because it reads the seconds on the computer to use as a seed value so that hitting it fast will give you a series of the same numbers as many times as you hit it w/in that second. so i wanted to make it pause after you attacked so people would not take advatage of this. i tried Sleep(); and Wait(); but when i #include <windows.h> or <windows> it gives me and error. i am using C++ and visual studio. I'd be grateful for any help.This is My code for the attacks so far...

while (EnemyHP > 0)
{
BattleActions();
damage = (enemyATT + (1 + (rand() % 4)) - defense);
cout << "The enemy dealt you " << damage << " physical damage.\n";
HP = (HP - damage);
DeathCheck();
}
You should only use srand() once, at the beginning of your program.
Are you using it more than once?
i only use srand(); once but should i be putting it in main or before main?, also i get random number's but usually rather large clumps of same number's for instance

(((1 + ((rand()) % 100)) + (1 + ((rand()) % 100))) / 2))
gives me a random number 1-100, by using two random number's divided by two, which was something i read about making percentages more reliable, apparantly they did it in Fire Emblem.
The problem is that when i compare this value to 50 i should get almost half accuracy. but it will get me M=miss H= Hit MMMMMMMMHHHHHHHMMMMMHHHHHMMMMMMMMHHHHHHHHMMMMMMHHHHH
as appossed to what i would really want which would be MMHMHMHMHMHMMHMHMHMHMHMHMHMHMHMHMHMHMHMHMMHMHMHMHMHM, or something even close to that.
One of the first things you do in main() is initialize the game -- which includes seeding the random number generator. For simple stuff:
1
2
3
4
int main()
  {
  srand( time( NULL ) );
  ...


The way you use the RNG makes a big difference.

Whatever you read about Fire Emblem's RNG was probably not what you thought it was.
http://fireemblem.wikia.com/wiki/Random_Number_Generator
http://faqs.ign.com/articles/520/520430p1.html
It has nothing to do with reliability, but simply the way the game calculates its stuff. As you can read in the article, the game actually reduces the randomness of the RNG in order to achieve some affects -- and which you can use to your advantage.

If you were to ask someone to write down a list of 20 random numbers (of his own choosing) from one to 100, it is very unlikely that he would alternate exactly between numbers less than 50 and greater than 50. So it is with true random numbers. And so it is with pseudo-random numbers. But that is how you are treating them.

You would likely get a better response by checking to see if the generated number is even or odd... but having not studied the RNG that is just a shot in the dark...

A much better method would be to check the time that the user initiated the action, and choose based upon the current number of milliseconds.

Hope this helps.
Last edited on
even with the use of srand the seeding is only down to the second, really to be truly random you need to write your own but this would beyond the scope of most of us.
For those of you who want to disagree with me do this simple test:

cut copy paste this into a file and run in console: (check code first tho, cause i'm going off the top of my head)

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

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include <iostream>
using namespace std;

int main()
{
    int randInt, count =0 ;

// seed random numbers using time;
    srand(time(NULL));

    while (i<2)
    {
        randInt = 1 + rand() % 50;
        cout << randInt < endl;
        count++;
    }
    return 0;
}


now if you run this once, it will produce 2 completely different numbers between 1 and 50.
run this program again a few seconds later and will produce 2 completely different numbers again.

BUT IF YOU SPAM the up arrow and the enter key on command line, it will run the program multiple times within 1 sec and give you the same numbers for each time the program is run within 1 second.

So knowing this, you need to adjust your code accordingly.

EDIT: so this is why you are seeing HHHHHH,MMMMMMMMM,HHHHHHHHH,HHHHHHHH,HHHHHHH,MMMMMMM,HHHHHHHH etc

however, using my while loop even though you will see the same numbers over and over the 2 numbers within the while loop will be different. So you can easily use srand to achieve your goals, you simply need to understand how the seeding works for you to be able to use it to your advantage.
Last edited on
No, it isn't why he is seeing that. (Did you bother to read my last response?)

If you continually reseed the RNG, then you are breaking it, and your random sequence will not be so random no matter what you do.

If you want a better RNG, there are many available on the web. (The C/C++ RNG is notoriously simple.)

However, the better way is to
 1. Use the RNG correctly, and
 2. Use the human playing the game as the RNG.
yeah when i said more reliable i just meant in the sense that it makes a 80% hit rate seem more like a 80% hit rate than a 60% hit rate. i think i got it with the only initating it once but we'll see once i implement it when i get a chance.
No, it isn't why he is seeing that. (Did you bother to read my last response?)


errrr I don't get it, didn't I just agree with you? and supply an example....

The way you use the RNG makes a big difference.


yep pretty sure that's what i said too.
Last edited on
btw when ever i use srand(time(NULL));
it always gives me this error

\my documents\visual studio 2008\homework folder\random numbers\random numbers\random numbers.cpp(19) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

anyone know why i get this?

btw here is my code for just random number's
#include <iomanip>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;



int main()
{
const int NUMBERS = 20;

double randvalue;
int i;

srand(time(NULL));

for (i=1;i <=NUMBERS;i++)
{
randvalue = (((1 + ((rand()) % 100)) + (1 + ((rand()) % 100))) / 2);
cout << randvalue << endl;
randvalue = (((1 + ((rand()) % 100)) + (1 + ((rand()) % 100))) / 2);
cout << randvalue << endl;



}

return 0;
}
FYI, that's not an error, it's a warning. You can ignore it (since you know what you are doing), or put this to remove it:

 
srand(unsigned int(time(NULL)));
Topic archived. No new replies allowed.