CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Changing a the stdlib.h file...

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
valemike
Guest







Changing a the stdlib.h file...
PostPosted: Thu Aug 26, 2004 7:57 am     Reply with quote

Currently, the rand() function uses stlib.h, and RAND_MAX is defined as:

[quote]
#ifndef RAND_MAX
#define RAND_MAX 32767 // The value of which is the maximum value
// ... returned by the rand function
#endif
[/endif]

Suppose I want only a random number between 0 and 16. May I change RAND_MAX to #define RAND_MAX 16 Question

Or should I refrain from touching any CCS header files and just do it the conventional way of taking a modulo-16?

i guess both ways work, but i'm not sure if anything else in CCS's suite uses this variable internally.

-Mike
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Aug 26, 2004 8:17 am     Reply with quote

I believe there is a function call srand() that will help generate a random number with the seed that you decide on, for example:

int i;

srand(16);// sets the new seed value
i = rand();// random number between 0 and 16

Ronald
valemike
Guest







PostPosted: Thu Aug 26, 2004 9:28 am     Reply with quote

Oh cool. If that is the case, then I just need to seed it with a max value of 16 using srand(). Initially, I thought that the argument in srand() had nothing to do with the maximum number i get with rand().

Only one way to find out - try it... Very Happy
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Aug 26, 2004 9:57 am     Reply with quote

I have never seen the random number generator seed used as a control for the span of the output. Please try it and tell us if it works.
_________________
The search for better is endless. Instead simply find very good and get the job done.
MikeValencia



Joined: 04 Aug 2004
Posts: 238
Location: Chicago

View user's profile Send private message Send e-mail Yahoo Messenger

Doesn't work
PostPosted: Thu Aug 26, 2004 10:50 am     Reply with quote

I tried it. Wrote the following code:

Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000) /* one instruction=1us? */
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <stdlib.h>

void main(void)
{
    unsigned long number;
    int i;
   
    srand(16);
    for (i=0; i<10; i++)
    {
        number = rand();
        printf ("%ld\r\n", number);
    }
}




I ran it, and printed out the results on hyperterminal:

Code:

7269
32262
12000
14030
18368
32741
29187
19196
12028
4768


Looks like I have to do a modulo.

-Mike
"valemike"
sh1



Joined: 04 Mar 2005
Posts: 5

View user's profile Send private message

PostPosted: Mon Jun 13, 2005 6:30 am     Reply with quote

You should do the following
Code:
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000) /* one instruction=1us? */
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define RAND_MAX 16
#include <STDLIB.H>

void main(void)
{
  unsigned long number;
  int i;

  srand(get_rtcc()); // this will create a new random seed based on the current time counter (wich is pretty random per se)

  for (i=0; i<10; i++)
  {
    number = rand();
    printf ("%ld\r\n", number);
  }
}
valemike
Guest







PostPosted: Mon Jun 13, 2005 11:27 am     Reply with quote

Thanks. I'm done with that project and am no longer at the place i consulted at. Can't put the code in now.

Anyhow, thanks for that info. I should've looked closely at the stlib.h file last year and found it was so easy to change RAND_MAX and the #ifndef prevents it from being re-defined.

-Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group