View Full Version : Random Numbers
Astronouth7303
18-01-2004, 20:29
Anybody know how to Generate a random number?
If you check the pdfs that came with the compiler (CD:\mcc18\doc\MPLAB-C18-Libraries.pdf), there is a random number type thing, but I can't use it!
Here:
//Prototype
int rand(void);
//...
/****************************************
* FUNCTION NAME: Random
* PURPOSE: Returns a random char
* CALLED FROM: Where ever
* ARGUMENTS: none
* RETURNS: unsigned char
****************************************/
unsigned char Random(void)
{
unsigned int rnd;
rnd = Rand;
rnd = rnd / RAND_MAX;
return (unsigned char) (rnd & 0xFF);
}
The thing is, When I build, I get
...\user_routines.c:267:Error [1105] symbol 'Rand' has not been defined :confused:
Help?!?
Dave Flowerday
18-01-2004, 20:36
You might want to try
rnd = rand();
And make sure you include stdlib.h in your user_routines.c file.
Astronouth7303
18-01-2004, 20:45
You might want to try
rnd = rand();
And make sure you include stdlib.h in your user_routines.c file.
Ok, I tried that. Get this: ...\user_routines.c:267:Warning [2058] call of function without prototype:
unsigned char Random(void)
{
unsigned int rnd;
// Right Here! ************************
rnd = Rand();
rnd = rnd / RAND_MAX;
return (unsigned char) (rnd & 0xFF);
}
And I included Std.lib.: #include <stdlib.h>
Other Half: ...\user_routines.c:289:Error [1131] type mismatch in assignment:
void Default_Routine(void)
{
//...
//Between Here...
pwm01=pwm03=Random;
pwm02=pwm04=Random;
//... And Here.
//...
}
P.S.- Tell me if it's some simple Syntax stuff. I'm still trying to get the hang of it. :o
Dave Flowerday
18-01-2004, 20:53
Ok, I tried that. Get this: ...\user_routines.c:267:Warning [2058] call of function without prototype:
rnd = Rand();
In C (and C++), function calls are case sensitive. Change "Rand()" to "rand()" and you should be all set.
pwm01=pwm03=Random;
pwm02=pwm04=Random;
Whenever you need to call a function, you need to include the parentheses, even if there aren't any arguments. So your code should look like:
pwm01=pwm03=Random();
pwm02=pwm04=Random();
Astronouth7303
18-01-2004, 20:57
In C (and C++), function calls are case sensitive. Change "Rand()" to "rand()" and you should be all set.
Whenever you need to call a function, you need to include the parentheses, even if there aren't any arguments. So your code should look like:
pwm01=pwm03=Random();
pwm02=pwm04=Random();
:cool: THANK YOU!! It compiles fine now.
P.S.- if you dig hard enough, there is an option in the compiler to turn off case-sensitivity :D but that would be nonconforming :rolleyes: oh, well.
Dave Flowerday
18-01-2004, 21:00
I'm quite curious as to why you'd want to set your PWM outputs to a random value, though. Most of us prefer to remove random behavior from our robots ;)
If you are setting the values of the PWMs to a random number everytime the program loop is executed (every ~26ms) .... the motors probably wont do anything ... because theres no way they can change speeds that quickly.
Astronouth7303
18-01-2004, 21:26
I'm quite curious as to why you'd want to set your PWM outputs to a random value, though. Most of us prefer to remove random behavior from our robots ;)
If you are setting the values of the PWMs to a random number everytime the program loop is executed (every ~26ms) .... the motors probably wont do anything ... because theres no way they can change speeds that quickly.
I did this mostly as a programming exercise. But you can use random moving as a defensive technique (grap goal & start random). Specifically, you probably wouldn't use it. But it might surprise your oponents a little.
The full code is:
//...
#include <stdlib.h>
//// DEFINE USER VARIABLES AND INITIALIZE THEM HERE
const int gTime = 75;
#define Left_Forward 0
#define Right_Forward 255
#define Left_Back 255
#define Right_Back 0
//...
//Prototype
int rand(void);
//...
/*******************************************
* FUNCTION NAME: Random
* PURPOSE: Returns a random char
* CALLED FROM: this file, Default_Routine
* ARGUMENTS: none
* RETURNS: char
*******************************************/
unsigned char Random(void)
{
unsigned int rnd;
rnd = rand();
rnd = rnd / RAND_MAX;
return (unsigned char) (rnd & 0xFF);
}
/*******************************************
* FUNCTION NAME: Default_Routine
* PURPOSE: Performs the default mappings of inputs to outputs for the
* Robot Controller.
* CALLED FROM: this file, Process_Data_From_Master_uP routine
* ARGUMENTS: none
* RETURNS: void
*******************************************/
//Random Mover
void Default_Routine(void)
{
static unsigned int i_cCount;
if (i_cCount > 1*gTime)
{
pwm01=pwm03=Random();
pwm02=pwm04=Random();
i_cCount = i_cCount;
}
i_cCount = i_cCount + 1;
printf("i_cCount=%d\n",(int)i_cCount);
} // END Default_Routine();
I shortened it for point of discussion. the //... means I removed something unimportant, or unmodified from the default code.
i_cCount = i_cCount;
Is that supposed to be "i_cCount = 0;"? Otherwise ... that line doesn't really do anything ... and your random code will never execute again.
Astronouth7303
18-01-2004, 21:39
i_cCount = i_cCount;
Is that supposed to be "i_cCount = 0;"? Otherwise ... that line doesn't really do anything ... and your random code will never execute again.
:o My bad. Thank you for pointing it out. You just saved me an hour of debugging. :o
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.