Go to Post Simply. Minimize. Reduce. It's a good engineering rule, and an even better management one. - Tom Line [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 18-01-2004, 20:29
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Question 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

Help?!?
  #2   Spotlight this post!  
Unread 18-01-2004, 20:36
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: Random Numbers

You might want to try
Code:
rnd = rand();
And make sure you include stdlib.h in your user_routines.c file.
  #3   Spotlight this post!  
Unread 18-01-2004, 20:45
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
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.
  #4   Spotlight this post!  
Unread 18-01-2004, 20:53
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
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();
  #5   Spotlight this post!  
Unread 18-01-2004, 20:57
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
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();
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   Spotlight this post!  
Unread 18-01-2004, 21:00
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
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   Spotlight this post!  
Unread 18-01-2004, 21:13
KevinB's Avatar
KevinB KevinB is offline
Registered User
AKA: Alum
no team
Team Role: Alumni
 
Join Date: Dec 2002
Rookie Year: 2001
Location: AL
Posts: 188
KevinB will become famous soon enoughKevinB will become famous soon enough
Send a message via ICQ to KevinB
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   Spotlight this post!  
Unread 18-01-2004, 21:26
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
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.
  #9   Spotlight this post!  
Unread 18-01-2004, 21:35
KevinB's Avatar
KevinB KevinB is offline
Registered User
AKA: Alum
no team
Team Role: Alumni
 
Join Date: Dec 2002
Rookie Year: 2001
Location: AL
Posts: 188
KevinB will become famous soon enoughKevinB will become famous soon enough
Send a message via ICQ to KevinB
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.
  #10   Spotlight this post!  
Unread 18-01-2004, 21:39
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
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.
My bad. Thank you for pointing it out. You just saved me an hour of debugging.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 17:56.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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