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 CCS Technical Support

rand(), Need to change RAND_MAX definition during program

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



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

rand(), Need to change RAND_MAX definition during program
PostPosted: Tue Jul 13, 2010 2:28 pm     Reply with quote

First I want to apologize for not being able to find this using the search machine. Seems like a fairly common request (changing definitions during runtime) I found a few things but it still doesn't make sense to me.

Long story short, I'm using the rand() function successfully. The only problem is, I want to be able to change the RAND_MAX definition during the program, but it doesn't seem to be working. I was trying something with the function below, but it is not working. I'm semi-aware that definitions are not supposed to be used like this during run-time, but I thought I'd give it a shot. The compiler didn't seem to mind. Any suggestions?

Code:


void check_seqs() {


      if(an0 <= 32)
      {
      #undef RAND_MAX
      #define RAND_MAX 1
      set_seq = 0;
      led_floater();
      output_low(PIN_B6);
      }
      else if(an0 > 32 && an0 <= 64)
      {
      #undef RAND_MAX
      #define RAND_MAX 2
      set_seq = 1;
      led_floater();
      output_low(PIN_A7);
      }
      else if(an0 > 64 && an0 <= 96)
      {
      #undef RAND_MAX
      #define RAND_MAX 3
      set_seq = 2;
      led_floater();
      output_low(PIN_A6);
      }
      else if(an0 > 96 && an0 <= 128)
      {
      #undef RAND_MAX
      #define RAND_MAX 4
      set_seq = 3;
      led_floater();
      output_low(PIN_C0);
      }
      else if(an0 > 128 && an0 <= 160)
      {
      #undef RAND_MAX
      #define RAND_MAX 5
      set_seq = 4;
      led_floater();
      output_low(PIN_C1);
      }
      else if(an0 > 160 && an0 <= 192)
      {
      #undef RAND_MAX
      #define RAND_MAX 6
      set_seq = 5;
      led_floater();
      output_low(PIN_C3);
      }
      else if(an0 > 192 && an0 <= 224)
      {
      #undef RAND_MAX
      #define RAND_MAX 7
      set_seq = 6;
      led_floater();
      output_low(PIN_C4);
      }
      else if(an0 > 224)
      {
      #undef RAND_MAX
      #define RAND_MAX 8
      set_seq = 7;
      led_floater();
      output_low(PIN_C5);
      }


}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 14, 2010 12:23 pm     Reply with quote

The source code for the CCS rand() routine is given in stdlib.h.
Look for the following text. The code is a short little routine that
occurs just after this line:
Quote:

The random number implementation

Copy the source to your program and rename the function slightly,
so it doesn't conflict with "rand()". Then edit your new function so
RAND_MAX is a variable in the function's parameter list, instead of being
a global constant. In other words, modify the function to suit your needs.
picj1984



Joined: 01 Mar 2010
Posts: 73

View user's profile Send private message

PostPosted: Thu Jul 15, 2010 1:49 pm     Reply with quote

PCM programmer,

You are brilliant. Can't believe I didn't think of that. Thanks!!
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