Be a Supporter!

rand () and probability

  • 583 Views
  • 11 Replies
New Topic Respond to this Topic
THEFlibberdigibbetX
THEFlibberdigibbetX
  • Member since: Feb. 27, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
rand () and probability 2006-07-24 07:46:27 Reply

I am trying to use rand () for probability of hitting in a game, but it doesn't seem to wrok. I have included the header cstdlib and ctime, I have forgotten to remove it, as I wasn't sure if I needed it, and my code for it is:

int hitrate;
int evarate;
hitrate = rand ();
evarate = rand ();
hitrate < ( enemyaccrightarm + 1 );
evarate < ( playerevahead + 1 );
double hit = hitrate / evarate;

if ( hit > 2 )
{
playerhphead --;
std::cout << "You have been hit, your new HP is " << playerhphead
<< std::endl;
}

where playerevahead && enemyaccrightarm == 8 and playerhphead == 15 and it just shows the same as if I had written:

while ( playerhphead > 1 )
{
playerhphead --;
std::cout << playerhphead << std::endl;
}

so it just goes down from 14 - 0

Any help would be appreciated, or a way to do probability in another way :)

THEFlibberdigibbetX
THEFlibberdigibbetX
  • Member since: Feb. 27, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to rand () and probability 2006-07-24 08:13:09 Reply

I should have said that this is in C++

JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to rand () and probability 2006-07-24 08:45:04 Reply

rand() will return a random number between 0 and RAND_MAX defined in php.ini unless parameters are given.

With parameters, the two optional arguments would be min and max

rand(3,8); //Generates random integer between 3 and 8 (inclusive)
rand(-7,43); //Generates random integer between -7 and 43 (inclusive)
rand(0,1); //Generates random integer of 0 or 1
-------------------------------------- ---------------------------------------
Currently, you are just giving the value of rand() to the variables, you probably need to declare numbers to go between.

JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to rand () and probability 2006-07-24 08:46:55 Reply

At 7/24/06 08:45 AM, JeremysFilms wrote: Lots of stuff

SHIT! I didn't notice your response post that it's in C++... even though the syntax is obviously C++ I must've somehow forgotten while posting... that was weird. I don't know any C++, but perhaps my post helps somehow?

Sorry.

THEFlibberdigibbetX
THEFlibberdigibbetX
  • Member since: Feb. 27, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to rand () and probability 2006-07-24 08:54:26 Reply

I have tried it as:

int hitrate = rand ( ( enemyaccrightarm / 2 ), enemyaccrightarm )
int evarate = rand ( ( playerevahead / 2 ), playerevahead )

and it doesn't work

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to rand () and probability 2006-07-24 08:57:38 Reply

in c++, i did a little expiramenting with rand() myself and found that basically rand returns a random short unsigned int. meaning between 0 and 32768.

I made a few functions for random numbers.

double random(){
return(rand()%32000)/32000.00000);
}

This one returns a random double-length floating point number between 0 and 1. Very useful base. Looking back, I realize i couldnt find the max of the rand function so i used the modulus. lol. It would probably work the same to just do rand()/32768

int random(int min, int max){
return static_cast<int>(floor((++max-min)*random(
))+min);
}

returns a random integer between max and min, uses the previous function too.

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to rand () and probability 2006-07-25 05:19:45 Reply

whenever using rand(), you will most likely need a call to srand() first

rand() is pseudo random numbers, and everytime youre application is run, it will return the same numbers, using srand() will seed the numbers so that you dont get the same set of random numbers again and again

for example

srand(time(NULL)) is often good enough for seeding the numbers


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to rand () and probability 2006-07-25 06:20:27 Reply

At 7/24/06 08:54 AM, THEFlibberdigibbetX wrote: and it doesn't work

try this :
int hitrate = (rand() % (enemyaccrightarm >> 1)) + (enemyaccrightarm >> 1)


"no sound in ass"

CronoMan
CronoMan
  • Member since: Jul. 19, 2004
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to rand () and probability 2006-07-25 06:22:42 Reply

At 7/25/06 05:19 AM, -dELta- wrote: srand(time(NULL)) is often good enough for seeding the numbers

You usually don't set seed when debugging, easier to debug an application that behaves the same way each time ;)


"no sound in ass"

FPSinsanity
FPSinsanity
  • Member since: Jul. 11, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to rand () and probability 2006-07-25 12:59:35 Reply

At 7/24/06 07:46 AM, THEFlibberdigibbetX wrote: I am trying to use rand () for probability of hitting in a game, but it doesn't seem to wrok. I have included the header cstdlib and ctime, I have forgotten to remove it, as I wasn't sure if I needed it, and my code for it is:

Leave both of those headers in there. Stdlib contains the rand() function and time contains the time() function which is used to create a random seed.

hitrate < ( enemyaccrightarm + 1 );
evarate < ( playerevahead + 1 );

These two lines don't do/set anything. What are you trying to do here? I'm guessing that you want the hitrate to be less than "enemyaccrightarm", and evarate to be less than "playerevahead". Here is how you would accomplish that:

int hitrate = rand()%enemyaccrightarm + 1; //Random number between 1 and enemyacc
int evarate = rand()%playerevahead +1; //Random number between 1 and playereva

Also, if there is no way to show when the code misses, you will only see the hits. The program runs very quickly, so it may seem like it hits every time. Do you have anything that displays when you miss?

THEFlibberdigibbetX
THEFlibberdigibbetX
  • Member since: Feb. 27, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to rand () and probability 2006-07-26 02:59:27 Reply

I will add them, and it displays the amount of HP after each attack, so I would know

THEFlibberdigibbetX
THEFlibberdigibbetX
  • Member since: Feb. 27, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to rand () and probability 2006-07-26 06:27:16 Reply

Problem solved

Now:
a) I am getting a random probability
and b) It now does show a miss