| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| fox85 Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				| unable to get RAND_MAX working |  
				|  Posted: Thu Nov 09, 2006 3:19 pm |   |  
				| 
 |  
				| I am trying to utilize the RAND_MAX function but it doesn't seem to do anything at all. I set up a test to try it out using a 16f690 pic chip with an LCD. 
 Here is my code:
 
  	  | Code: |  	  | #include <16F690.h>
 #device ADC=8
 #include <STDLIB.h>
 #fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN
 #use delay(clock=8000000)
 #define RAND_MAX 12
 #use RS232(Baud=9600,Xmit=PIN_A0)
 
 
 void test_randoma(int x){
 
 int i;
 long randx;
 
 randx = rand();
 
 printf("?f");
 delay_ms(200);
 printf("test random A");
 delay_ms(500);
 
 
 for(i=0; i<x; i++){
 
 randx = rand();
 
 printf("?f");
 delay_ms(200);
 printf("%5Lu?a",randx);
 delay_ms(1000);
 
 }
 }
 
 void test_randomb(int x){
 
 
 int i;
 long randx;
 
 #define RAND_MAX 12
 randx = rand();
 
 printf("?f");
 delay_ms(200);
 printf("test random B");
 delay_ms(1000);
 
 
 for(i=0; i<x; i++){
 
 randx = rand();
 
 printf("?f");
 delay_ms(200);
 printf("%5Lu?a",randx);
 delay_ms(1000);
 
 }
 }
 
 void main(void){
 #define RAND_MAX 12
 
 printf("?B7F"); // power contrast pin on LCD
 delay_ms(500);
 printf("?f");
 delay_ms(5000);
 printf("beginning test");
 delay_ms(1000);
 
 while(1){
 
 test_randoma(5);
 delay_ms(1500);
 test_randomb(5);
 
 }
 }
 | 
 In this code I used the random call as a long but I also tested it as an int. The code runs fine printing  random long values to the LCD but  RAND_MAX doesn't cap the random numbers  at all. Any suggestions?
 |  |  
		|  |  
		| fox85 Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				| RAND_MAX |  
				|  Posted: Thu Nov 09, 2006 3:22 pm |   |  
				| 
 |  
				| Also, #define RAND_MAX appears twice in this code however I have tried it with #define both only at the top and only in the body of main and neither made any difference. |  |  
		|  |  
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Nov 09, 2006 3:31 pm |   |  
				| 
 |  
				| Look in this file that you've included: 
  	  | Code: |  	  | #include <STDLIB.h> | 
 
 Near the start of the file, it defines RAND_MAX:
 
  	  | Code: |  	  | #ifndef RAND_MAX
 #define RAND_MAX  32767
 #endif
 
 | 
 
 The #ifndef statement means it will use the default value, provided
 that RAND_MAX hasn't been previously defined, prior to this point.
 So if you want to change the default value of RAND_MAX (which is
 32767), you need to put in a #define statement with a new value,
 and you need to place the statement before the one in stdlib.h.
 Currently, you have several of them in random areas of your program.
 You should delete those lines and do it like this:
 
  	  | Code: |  	  | #define RAND_MAX 12
 #include <stdlib.h>
 
 | 
 |  |  
		|  |  
		| fox85 Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				| thanks for the help, working now |  
				|  Posted: Thu Nov 09, 2006 3:43 pm |   |  
				| 
 |  
				| Thanks, PCM programmer. I had looked over the stdlib file but didn't make the connection regarding the order of my statements in the top. Thanks. |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |