|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
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 Help?!? |
|
#2
|
||||
|
||||
|
Re: Random Numbers
You might want to try
Code:
rnd = rand(); |
|
#3
|
|||||
|
|||||
|
Re: Random Numbers
Quote:
Code:
unsigned char Random(void)
{
unsigned int rnd;
// Right Here! ************************
rnd = Rand();
rnd = rnd / RAND_MAX;
return (unsigned char) (rnd & 0xFF);
}
Other Half: ...\user_routines.c:289:Error [1131] type mismatch in assignment: Code:
void Default_Routine(void)
{
//...
//Between Here...
pwm01=pwm03=Random;
pwm02=pwm04=Random;
//... And Here.
//...
}
![]() |
|
#4
|
||||
|
||||
|
Re: Random Numbers
Quote:
Quote:
Code:
pwm01=pwm03=Random(); pwm02=pwm04=Random(); |
|
#5
|
|||||
|
|||||
|
Re: Random Numbers
Quote:
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 but that would be nonconforming oh, well. |
|
#6
|
||||
|
||||
|
Re: Random Numbers
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
![]() |
|
#7
|
||||
|
||||
|
Re: Random Numbers
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.
|
|
#8
|
|||||
|
|||||
|
Re: Random Numbers
Quote:
Quote:
The full code is: Code:
//...
#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();
|
|
#9
|
||||
|
||||
|
Re: Random Numbers
Code:
i_cCount = i_cCount; |
|
#10
|
|||||
|
|||||
|
Re: Random Numbers
Quote:
My bad. Thank you for pointing it out. You just saved me an hour of debugging. ![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A couple of noodle scratchers | Cheese Head | Programming | 11 | 07-12-2002 09:59 |
| Even more interesting numbers: Division of regional winners | archiver | 2001 | 8 | 24-06-2002 03:10 |
| More interesting numbers...specific to big-ball matches | archiver | 2001 | 13 | 24-06-2002 02:51 |
| How random is random???? | archiver | 1999 | 0 | 23-06-2002 21:59 |
| Help i need match numbers... | Drmoofdaddy | Championship Event | 5 | 26-04-2002 18:03 |