View previous topic :: View next topic |
Author |
Message |
filjoa
Joined: 04 May 2008 Posts: 260
|
use rand function |
Posted: Mon Mar 15, 2010 4:41 pm |
|
|
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
|
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Mon Mar 15, 2010 7:01 pm |
|
|
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
|
|
Posted: Mon Mar 15, 2010 7:45 pm |
|
|
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
|
|
Posted: Tue Mar 16, 2010 8:01 am |
|
|
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
|
|
Posted: Tue Mar 16, 2010 9:59 am |
|
|
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
|
|
Posted: Tue Mar 16, 2010 11:36 am |
|
|
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
|
|
Posted: Tue Mar 16, 2010 1:44 pm |
|
|
Hi
Thanks, it work and my last program too, my problem stay on USB-RS232 converter....
Thanks all |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Tue Jul 27, 2010 3:26 pm |
|
|
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: 19552
|
|
Posted: Wed Jul 28, 2010 2:17 am |
|
|
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 |
|
|
|