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

use rand function

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



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

use rand function
PostPosted: Mon Mar 15, 2010 4:41 pm     Reply with quote

Hi

I try today use rand() function but without success.
Code:

#include <18F252.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,BROWNOUT,STVREN
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#define RAND_MAX 50

int8 count,i=0;

void main()
{

while(TRUE)
  {
   i = input(PIN_B0);

   if (i==1)
    {
     srand(5);
     count=rand();
     printf("Numero: %d\n\r",count);
    }

   i=0;
  }
}

My idea is to write 5 random numbers between 1 and 50, but it don't work. Someone know why?

best regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 15, 2010 4:59 pm     Reply with quote

Look at how srand() and rand() are used in this sample program:
http://www.ccsinfo.com/forum/viewtopic.php?t=28199&highlight=rand+srand&start=1
Note the location in the program where srand() is used. ie., above the
loop.
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Mon Mar 15, 2010 7:01 pm     Reply with quote

hi

I edit my program to this:
Code:

void main()
{
int8 b;

srand(5);

while(TRUE)
  {
   i = input(PIN_B0);

   if (i==1)
     {
      for (b=0;b<=5;b++)
        {
         count = rand();
         printf("Numero: %2u\n\r",count);
        }
     }
   i=0;
   }
}


and on terminal only appear "Numero: " not appear any result of count.

best regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 15, 2010 7:45 pm     Reply with quote

It works in MPLAB simulator with compiler vs. 4.105. I had to put the
#define for RAND_MAX above the #include line for stdlib.h, to get it to
compile. I also removed the input(PIN_B0) line, and set i = 1.
(To make it work in the simulator). Then it gives this output:
Quote:

Numero: 5
Numero: 25
Numero: 16
Numero: 27
Numero: 11
Numero: 32
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Tue Mar 16, 2010 8:01 am     Reply with quote

hi

Now I not stay at home but at night I try this.

One question, when I write "srand(5)" program generate 5 numbers correct? How I can see this numbers? It stay on array or something?
or when I write this it only generate 5 diferent numbers, when I use next 5 count=rand()?

best regards
collink



Joined: 08 Jan 2010
Posts: 137
Location: Michigan

View user's profile Send private message Visit poster's website

PostPosted: Tue Mar 16, 2010 9:59 am     Reply with quote

filjoa wrote:
hi

Now I not stay at home but at night I try this.

One question, when I write "srand(5)" program generate 5 numbers correct? How I can see this numbers? It stay on array or something?
or when I write this it only generate 5 diferent numbers, when I use next 5 count=rand()?

best regards


No. rand will output a new number every time you call it (and do this forever), srand has nothing to do with that. srand means "seed random number generator". For every seed value there is a predefined progression of random numbers. You see, they aren't random really. To get a different list of random numbers you need to pass a different number to srand. On desktop machines people usually pass the current system time to srand so that the output is different every time the program is run. Unless you've got a RTC you aren't going to be able to do this. If you want a different progression of numbers every time you run the program you'll need to find something that is not the same on program start. Maybe the value of an analog input or something like that.
languer



Joined: 09 Jan 2004
Posts: 144
Location: USA

View user's profile Send private message

PostPosted: Tue Mar 16, 2010 11:36 am     Reply with quote

filjoa,

Below is a similar program for the 12F683 (compiler ver. 4.104)...

Code:
// Preprocessor
#include <12F683.h>
#device *=16 ADC=10
#fuses INTRC_IO,NOWDT,NOPUT,NOPROTECT,NOCPD,NOMCLR
#use delay(clock=8000000)
#use rs232 (baud=9600,xmit=PIN_A0,parity=N,bits=8)

// Libraries
#define RAND_MAX 50
#include <STDLIB.H>

int8 count,b;

void main()
{
   /* Initialize HW */
   setup_comparator(NC_NC_NC_NC);      // disable comparators
   setup_adc_ports(NO_ANALOGS);      // select AN0 for input, and Vdd for reference
   setup_adc(ADC_OFF);               // set adc clock

   /* Initital Random Seed */
   srand(5);                     // everytime this is called the seed will be initialized and the random sequence will be started

   /* Main Program */
   while (TRUE)
   {
      if (input(PIN_A1))
      {
         for (b=0;b<=5;b++)
         {
            count=rand();
            printf("Numero: %d\n\r",count);
         }
      }
   }
}

The first five outputs are:
Quote:
Numero: 5
Numero: 25
Numero: 16
Numero: 27
Numero: 11
Numero: 32
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Tue Mar 16, 2010 1:44 pm     Reply with quote

Hi

Thanks, it work and my last program too, my problem stay on USB-RS232 converter.... Evil or Very Mad Evil or Very Mad


Thanks all Wink
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

PostPosted: Tue Jul 27, 2010 3:26 pm     Reply with quote

Hi

I return to this post because I stay with same problem with rand function...

My program should return a number between 1 and 6, but it only return number 2, someone know why?
Code:

#include <12F683.h>
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, MCLR, PUT, NOBROWNOUT, NOIESO, NOFCMEN
#use delay(clock=8000000)

#define RAND_MAX 6
#include <STDLIB.H>

/*
0x01 --> 1
0x02 --> 2
0x03 --> 3
0x06 --> 4
0x07 --> 5
0x16 --> 6
*/

void main()
{
int8 n,c;

srand(1);

n = rand();

switch (n)
{
   case 1 : output_a(0x01);      // 1
            break;
   case 2 : output_a(0x02);      // 2
            break;
   case 3 : output_a(0x03);      // 3
            break;
   case 4 : output_a(0x06);      // 4
            break;
   case 5 : output_a(0x07);      // 5
            break;
   case 6 : output_a(0x16);      // 6
            break;
}

}


PS: if I remove "srand(1)" I think it work but my 1ยบ number is always 4 :S I don't understand why...

best regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Jul 28, 2010 2:17 am     Reply with quote

The generators used in just about all languages, are _pseudo random number sequence generators_. They do _not_ produce random numbers, but a sequence of numbers whose probability distribution is 'close' to that of real random numbers. The starting point of the sequence, is setup by the 'srand' function. Unless this is used, the number sequence will _always_ start at the same point, giving the same sequence. This is what you are seeing.
Now there is no hardware in the PIC as standard to give you any form of real random number. The closest you can get, is to add something like an RTCC chip, and read the time from this when the processor wakes up, then use this value to generate a CRC, and this to seed 'srand'. Then, unless the unit is switched on at _exactly_ the same moment in time, the sequence will start at a different point.
Generating 'real' random numbers is surprisingly hard. Do some reading on this.....

Best Wishes
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