Server Help

Bot Questions - random setfreq rather than a static number

Anonymous - Wed Aug 18, 2004 2:57 pm
Post subject: random setfreq rather than a static number
i'm editing the tv vex plugin, and it sets kicked people to freq 0 initially, i changed this to 85, but now i want this to be a random freq in 3-99 instead

sendPrivate(parse->item, "*setfreq 85");

thats what it is right now, i've read over the mervbot tutorial, with the section on random numbers and players, but it failed to work, and yes, i did include time.h tongue.gif

my guess is that my syntax was wrong or something, this is what i thought it should have been:

srand(time(NULL));
rand();
int tempkickofffreq = (int) (99 * ((float)rand()/RAND_MAX));
sendPrivate(parse->item, "You were kicked from the freq");
sendPrivate(parse->item, "*setfreq " + tempkickofffreq);

tho that would do 0-99 correct? (still didn't work btw)
i also tried putting the last tempkickofffreq as: int(tempkickofffreq)

and vegito suggested this to me:

srand(time(NULL));
int Random = rand()%51 + 1;
sendPrivate(parse->item, "You were kicked from the freq");
sendPrivate(parse->item, "*setfreq " + Random);

none have worth thus far, i'm hoping someone here will know what i'm talking about and can correct me

ty in advanced
Solo Ace - Wed Aug 18, 2004 3:25 pm
Post subject:
Code: Show/Hide
inline int randrange(int low, int high)
{
    return rand() / (RAND_MAX / (high - low + 1)) + low;
}


It crashed my program when I passed values with a too big difference to it I think, but I guess you won't have such problems.
Cyan~Fire - Wed Aug 18, 2004 4:49 pm
Post subject:
Should work:
Code: Show/Hide
rand() * 97 + 2

Underlord - Wed Aug 18, 2004 5:33 pm
Post subject:
sendPrivate(parse->item, "*setfreq " + tempkickofffreq);

should be:

sendPrivate(parse->item, "*setfreq " + (String) tempkickofffreq);
Anonymous - Fri Aug 27, 2004 12:03 pm
Post subject:
ty you three, i'll try yours first underlord
Mr Ekted - Fri Aug 27, 2004 12:35 pm
Post subject:
rand() % 97 + 3 will result in numbers from 3 to 99
D1st0rt - Fri Aug 27, 2004 2:32 pm
Post subject:
rand % 97? you mean rand() * 97?
Dr Brain - Fri Aug 27, 2004 2:34 pm
Post subject:
No, rand gives an int. The modulo operator (%) will take the remainder when devided by 97. Means you get a random number from 0 to 96.

+3 gives the proper offset.
D1st0rt - Fri Aug 27, 2004 2:44 pm
Post subject:
I thought rand gave a decimal between 0 and 1
Deadly - Fri Aug 27, 2004 3:33 pm
Post subject:
blah, that random stuff confuses me.. i tried making a lottery plugin didnt work, gay random

its wierd.. it chooses a random number, then i make it say "*arena Number: " +(String)temp or whatever, for testing so i can guess the number and see if it works.. but it didnt work, i think when it sends the arena its choosing a new number by doing +(String)temp..

Code: Show/Hide
         if(countdown[0] == 14){
            srand(time(NULL));
             rand();
            int temp = (int) (51 * ((float)rand()/RAND_MAX));

            sendPublic("*arena Number for this round is " +(String)temp +".");
         }


if someone can help me out with that.. that would be good tongue.gif
Underlord - Fri Aug 27, 2004 4:09 pm
Post subject:
that code works fine deadly

rand() returns an int between 0 and RAND_MAX (32767)

never use something like 'rand() % 97'. The way the random number generator works, doing that will give 'less random' numbers, and often cycle between odd/even numbers.
Cyan~Fire - Fri Aug 27, 2004 6:12 pm
Post subject:
Ooops. I got confused with BASIC for a sec there. icon_sad.gif

Underlord wrote:
never use something like 'rand() % 97'. The way the random number generator works, doing that will give 'less random' numbers, and often cycle between odd/even numbers.

I've never noticed a problem doing it that way (which is always how I do it).
D1st0rt - Fri Aug 27, 2004 7:23 pm
Post subject:
shows how much I know, in Java Math.random() returns a double between 0 and 1
Mr Ekted - Fri Aug 27, 2004 8:34 pm
Post subject:
Underlord wrote:
that code works fine deadly

rand() returns an int between 0 and RAND_MAX (32767)

never use something like 'rand() % 97'. The way the random number generator works, doing that will give 'less random' numbers, and often cycle between odd/even numbers.


That's retarded. If rand() is unreliable, then you should not use it at all. Going through a floating point conversion of it, makes it less efficient also, but doesn't make it any more random. If you don't like rand(), then download a better RNG, but always use the MOD operator.
Deadly - Fri Aug 27, 2004 10:09 pm
Post subject:
Underlord wrote:
that code works fine deadly


ok.. well the way i ahve it is when a player types !guess <number> it stores set_tag(p, GUESS, playersnumber), then when time is over, it goes through the tag list, taking everyones number, and seeing if it matches by doing

Code: Show/Hide

if(get_tag(p, GUESS) == temp){
winner = p->name;
}


Didnt seem to work icon_sad.gif
Cyan~Fire - Fri Aug 27, 2004 11:32 pm
Post subject:
Can we see a little more of that code, please?
50% Packetloss - Sat Aug 28, 2004 12:09 am
Post subject:
Uint16 random_range(Uint16 lowest_number, Uint16 highest_number)
{
if(lowest_number > highest_number){
swap(lowest_number, highest_number);
}
Uint16 range = highest_number - lowest_number + 1;
return lowest_number + Uint16(range * rand()/(RAND_MAX + 1.0));
}


this is the function that i have used to do random ranges. I think i got it off a website. Its more efficient to just use the modulus operator.
Anonymous - Sat Aug 28, 2004 12:34 am
Post subject:
Merv comes with a prng
Anonymous - Fri Sep 03, 2004 1:03 pm
Post subject:
ok, this is still failing to work for me

Quote:

if (name == parse->item->name)
{
if (parse->item->team != i)
{
sendPrivate(p, "The player is not on the freq");
return;
}
srand(time(NULL));
rand();
int tempkickofffreq = (int) (99 * ((float)rand()/RAND_MAX));
sendPrivate(parse->item, "You were kicked from the freq");
sendPrivate(parse->item, "*setfreq " + (String) tempkickofffreq);
return;
}




that what i have so far

Quote:

command.cpp(556) : error C2664: 'void __thiscall botInfo::sendPrivate(struct Player *,char *)' : cannot convert parameter 2 from 'class String' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.


if anyone could help me, it would be great, if u can't, i understand
Cyan~Fire - Fri Sep 03, 2004 2:16 pm
Post subject:
Replace
Code: Show/Hide
sendPrivate(parse->item, "*setfreq " + (String) tempkickofffreq);

with
Code: Show/Hide
sendPrivate(parse->item, String("*setfreq " + (String) tempkickofffreq)).msg;

Should work.

Really it's better do do something like:
Code: Show/Hide
String message("*setfreq");
message += tempkickofffreq;
sendPrivate(parse->item, message.msg);

More lines of C++, less lines of ASM (without optimization).
Anonymous - Thu Oct 21, 2004 4:18 pm
Post subject:
i know this is an old thread, but thank you very much cyan~fire, what you posted worked perfectly

if anyone knows how to make this 2-99, that would be great:
Code: Show/Hide

int tempkickofffreq = (int) (99 * ((float)rand()/RAND_MAX));

Cyan~Fire - Thu Oct 21, 2004 5:48 pm
Post subject:
Wow, you really didn't understand anything we told you?

First of all, rand() returns an int. Why are you float-ing it?

Second, learn what the % operator does and then modify the code yourself (to use it).
Underlord - Thu Oct 21, 2004 9:25 pm
Post subject:
Code: Show/Hide

srand(time(NULL));
rand();
int tempkickofffreq = (int) (97 * ((float)rand()/RAND_MAX)) + 2;

Dr Brain - Thu Oct 21, 2004 10:36 pm
Post subject:
Code: Show/Hide
int tempkickofffreq = (rand() % 97) + 2;

Anonymous - Thu Oct 21, 2004 10:51 pm
Post subject:
Again, thank you for the resonses

@Cyan~Fire:
I'm very sorry I couldn't comprehend much from reading some lines of code involving the simplicities of random numbers. I'm always thankful for the help, but nothing provided to me so far has actually worked (what you gave me compiled fine but doesn't work when the command is sent :S) but I'm not complaining, I'm going to continue to use the items given here and hopefully get it working properly.
Bak - Thu Oct 21, 2004 11:12 pm
Post subject:
Code: Show/Hide
// Seed random number generator (do once when program starts)
void seedRandom()
{
   srand(time(0));
}

// Get a random number between lowerBound and upperBound, inclusive
// so getRandomNumber(2,4) will return 2, 3, or 4
// if your range is invalid (lowerBound < upperBound) returns -1
int getRandomNumber(int lowerBound, int upperBound)
{
   int range = upperBound - lowerBound + 1;
   int rv = -1;

   if (range > 0)
  {
    rv = lowerBound + rand() % range;
  }
   
  return rv;
}


And make sure in your code you call seedRandom() once when the program starts (or in the botInfo constructor) and use getRandomNumber(2,85); to get a random number between 2 and 85, inclusive.

so like:

sendPrivate(p,"*setfreq " + (String)getRandomNumber(2,85));

where p is a Player*
Mine GO BOOM - Fri Oct 22, 2004 9:21 am
Post subject:
Code: Show/Hide
// Seed random number generator (do once when program starts)
void seedRandom()
{
   srand(time(0));
}

// Get a random number between lowerBound and upperBound, inclusive
// so getRandomNumber(2,4) will return 2, 3, or 4
// if your range is invalid (lowerBound < upperBound) returns -1
int getRandomNumber(int lowerBound, int upperBound)
{
   int range = upperBound - lowerBound + 1;
   int rv;

   if (range > 0)
    rv = lowerBound + (int)(range * ((float)rand() / RAND_MAX));
   else if (range == 0)
    rv = lowerBound;
   else
     rv = -1;
   
  return rv;
}


A slightly better randomizer. rand() returns with its higher bits more randomizer than its lower bits. So doing 2*(rand()/RAND_MAX) will return an average of 0.5, while rand()%2 may return 0.7 after 1000 trials.
Bak - Fri Oct 22, 2004 1:43 pm
Post subject:
Code: Show/Hide
// Stan bak

#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

#define RUNS 1000000

int main()
{
   int numGreater = 0;
   int numLess = 0;
   int num, x;

   srand((int)time(0));

   for (x = 0; x < RUNS;++x)
   {
      num = rand() % 128;

      if (num >= 64)
         numGreater++;
      else
         numLess++;
   }

   cout << "Num less: " << numLess << endl;
   cout << "Num greater: " << numGreater << endl;

   return 0;
}


Output:

TRIAL 1
Num less: 499908
Num greater: 500191

TRIAL 2
Num less: 500224
Num greater: 499776

TRIAL 3
Num less: 500058
Num greater: 499942

TRIAL 4
Num less: 499350
Num greater: 500650

maybe there's different versions of rand?
Mr Ekted - Fri Oct 22, 2004 2:52 pm
Post subject:
Thank you Bak for your sanity in times of chaos (MGB and UL). icon_smile.gif
Cyan~Fire - Fri Oct 22, 2004 5:24 pm
Post subject:
Code: Show/Hide
#include <iostream>

You call that sanity?!
Mr Ekted - Fri Oct 22, 2004 7:02 pm
Post subject:
I was willing to ignore that transgression for the greater good. icon_smile.gif
Underlord - Fri Oct 22, 2004 7:31 pm
Post subject:
Code: Show/Hide
The rand() function in the Microsoft C library v4.0.
         Definition:
         SEED = (214013*SEED + 2531011) mod 2**31
   X = int( SEED/2**16 )
   Returns integer in range [0, 2**15)     {0 inclusive, 2**15 exclusive}


that's a normal linear congruential generator.
y = rand() % M; focuses on the lower bits of rand(), so the lower bytes are much less random than the higher bytes. The lowest bit cycles between 0 and 1.

there's different versions of rand(), so need to know which bak is using. should always use the:

(int) (97 * ((float)rand()/RAND_MAX))

way unless you are sure your rand() function is safe

did some tests awhile back on vc++ 6.0 stdlib rand(), it gave chi square distribution 0.01% (<1% = nonrandom), arithmetic mean 52 (127 = random), monte carlo 4 (should be pi)

http://home1.gte.net/deleyd/random/random4.html#CRAND
http://www.fourmilab.ch/random/

bigshot: long as u do srand then either rand() method, u should be fine. don't need super random numbers for a freq changing bot. post your source if you still can't get it to work
50% Packetloss - Fri Oct 22, 2004 9:23 pm
Post subject:
Just hook your bot up to this random number generator

Dr Brain - Fri Oct 22, 2004 9:39 pm
Post subject:
How random "random" really is doesn't matter in this case. It's not like he's using this for encryption.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group