Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Random Numbers (http://www.chiefdelphi.com/forums/showthread.php?t=24059)

Astronouth7303 18-01-2004 20:29

Random Numbers
 
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

Re: Random Numbers
 
You might want to try
Code:

rnd = rand();
And make sure you include stdlib.h in your user_routines.c file.

Astronouth7303 18-01-2004 20:45

Re: Random Numbers
 
Quote:

Originally Posted by Dave Flowerday
You might want to try
Code:

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:

Code:

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:

Code:

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

Re: Random Numbers
 
Quote:

Originally Posted by Astronouth7303
Ok, I tried that. Get this: ...\user_routines.c:267:Warning [2058] call of function without prototype:

Code:

  rnd = Rand();

In C (and C++), function calls are case sensitive. Change "Rand()" to "rand()" and you should be all set.

Quote:

Code:

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:
Code:

pwm01=pwm03=Random();
pwm02=pwm04=Random();


Astronouth7303 18-01-2004 20:57

Re: Random Numbers
 
Quote:

Originally Posted by Dave Flowerday
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:
Code:

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

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 ;)

KevinB 18-01-2004 21:13

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.

Astronouth7303 18-01-2004 21:26

Re: Random Numbers
 
Quote:

Originally Posted by Dave Flowerday
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 ;)

Quote:

Originally Posted by KevinB
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:
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();

I shortened it for point of discussion. the //... means I removed something unimportant, or unmodified from the default code.

KevinB 18-01-2004 21:35

Re: Random Numbers
 
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

Re: Random Numbers
 
Quote:

Originally Posted by KevinB
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.

:o My bad. Thank you for pointing it out. You just saved me an hour of debugging. :o


All times are GMT -5. The time now is 21:18.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi