Mine GO BOOM wrote: |
If you want to pick a random spot inside of a circle, you choose two random values. One value is anywhere between 0 and the radius of the circle. The next value is anything between 0 and 2*pi. Now, using trigonometry, convert those polar coordinates into rectangular. Then add those X and Y values to the center point of the circle.
To help you learn this, I'll leave out the math for converting polar to rectangular. Use google or a math book to work that one out. |
Quan Chi2 wrote: |
yesss ![]() |
Code: Show/Hide void PickRandomSpotInCircle(int CircleX, int CircleY, int Radius, int &XCoord, int &YCoord)
{ double theta, r; r = (double)rand() / RAND_MAX * Radius; theta = (double)rand() / RAND_MAX * 6.28319; /* aka: 2pi */ XCoord = (int)(r * cos(theta)) + CircleX; YCoord = (int)(r * sin(theta)) + CircleY; } |
Quan Chi2 wrote: |
hey lol id you make that up yourself? looks cool lol seriously :P im looking up some things in that code to learn how it works hehe |
Quan Chi2 wrote: |
um.. im supposed to fill in the coords and stuff right? |
Code: Show/Hide #include "time.h" // do srand(time(NULL)); when the bot logins or just once at any point before rand(); is called void botInfo::PutPilotInGame(Player *p) { // warp pilot into game, 100 square area from coordinate 450,450 rand(); int coordx = 450; int coordy = 450; coordx = (int) (100 * ((float)rand()/RAND_MAX)) + 450; coordy = (int) (100 * ((float)rand()/RAND_MAX)) + 450; // warpto sendPrivate(p,"*warpto " + (String) coordx + " " + (String) coordy); } |
Code: Show/Hide 100 * ((float)rand()/RAND_MAX) |
Code: Show/Hide rand() % 100 |
Quote: |
Do NOT use
y = rand() % M; as this focuses on the lower bits of rand(). For linear congruential random number generators, which rand() often is, the lower bytes are much less random than the higher bytes. In fact the lowest bit cycles between 0 and 1. Thus rand() may cycle between even and odd (try it out). Note rand() does not have to be a linear congruential random number generator. It's perfectly permissible for it to be something better which does not have this problem. |
Code: Show/Hide rand() % 2 |
Code: Show/Hide (rand() % 10000) / 5000 |
Code: Show/Hide // do srand(time(NULL)); when the bot logins or just once at any point before rand(); is called |
Code: Show/Hide #include <stdlib.h>
#include <time.h> #include <math.h> /* Call this somewhere */ srand(time(NULL)); /* Later on (Angle is in radians) */ int X, Y; double Angle, Mag; Angle = (rand()/RAND_MAX)*6.283185307; Mag = sqrt(((float)rand())/(float)RAND_MAX)*radius; X = (int)(cos(Angle) * Mag); Y = (int)(sin(Angle) * Mag); |
Code: Show/Hide case OP_Player: { // Player-level commands if (c->check("about")) { sendPrivate(p, "I am a plain vanilla flavored bot. Yup, just as plain and useless as can be."); } if (c->check("confuse")) { srand(time(NULL)); int X, Y; double Angle, Mag; Angle = (rand()/RAND_MAX)*6.283185307; Mag = sqrt(((float)rand())/(float)RAND_MAX)*10; X = (int)(cos(Angle) * Mag); Y = (int)(sin(Angle) * Mag); { String s; s += "*warpto"; s += " X Y"; sendPrivate(p, s.msg); sendPrivate(p, "You have dodged your opponents weapons! Now go for the kill!"); } } } } } |
Code: Show/Hide #include <stdlib.h>
#include <time.h> #include <math.h> /* Call this somewhere */ srand(time(NULL)); /* Later on (Angle is in radians) */ int X, Y; double Angle, Mag; Angle = (rand()/RAND_MAX)*6.283185307; Mag = sqrt(((float)rand())/(float)RAND_MAX)*radius; X = (int)(cos(Angle) * Mag); Y = (int)(sin(Angle) * Mag); |
Code: Show/Hide int round(double n)
{ if (n >= 0.0) return (int)(n + 0.5); else return (int)(n - 0.5); } |
Code: Show/Hide case OP_Player: { // Player-level commands if (c->check("about")) { sendPrivate(p, "I am a plain vanilla flavored bot. Yup, just as plain and useless as can be."); } if (c->check("confuse")) { srand(time(NULL)); rand(); int X, Y; double Angle, Mag; Angle = (rand()/RAND_MAX)*6.283185307; Mag = sqrt(((float)rand())/(float)RAND_MAX)*10; X = (int)(cos(Angle) * Mag); Y = (int)(sin(Angle) * Mag); { sendPrivate(p, "*warpto "+String(X)+" "+String(Y)); sendPrivate(p, "You have dodged your opponents weapons! Now go for the kill!"); } } } } } |
Code: Show/Hide X = X + p->tile.x; Y = Y + p->tile.y; |
Maverick wrote: |
Fixed code that should work:
... Also notice that you must use rand(); once after using srand(); |
Mine GO BOOM wrote: |
No, bad. Don't keep reseeding srand, you should only need to do that once at the start of your program. |
Mine GO BOOM wrote: |
And didn't you read anything I wrote up in my fancy post either? Typecasting to int is bad for this, do some rounding function like the one I have listed above. |