为什么';在PIC模拟器IDE中没有随机值吗?
我使用PIC16F886的PIC模拟器IDE,每次运行代码时,random总是等于0。看起来为什么';在PIC模拟器IDE中没有随机值吗?,c,microcontroller,pic,C,Microcontroller,Pic,我使用PIC16F886的PIC模拟器IDE,每次运行代码时,random总是等于0。看起来rand()不起作用。我不明白发生了什么事 代码如下: #include <16F886.h> #include <stdlib.h> #include <time.h> #include <stdio.h> #FUSES NOWDT #FUSES PUT #FUSES NOMCLR #FUSES NOPROTECT #FUSES NOCPD #FUSES
rand()
不起作用。我不明白发生了什么事
代码如下:
#include <16F886.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#FUSES NOWDT
#FUSES PUT
#FUSES NOMCLR
#FUSES NOPROTECT
#FUSES NOCPD
#FUSES BROWNOUT
#FUSES IESO
#FUSES FCMEN
#FUSES NOLVP
#FUSES NODEBUG
#FUSES NOWRT
#FUSES BORV40
#FUSES RESERVED
#FUSES INTRC_IO
#use delay(clock=8M)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
void main()
{
int myInput;
int random;
random = rand() % 5;
printf("type a character\r\n");
printf(" %d ", random);
while(1) {
myInput = getc();
printf("You typed - %d\r\n", myInput);
if(myInput>random){
printf("too high\n");
}
else if(myInput<random) {
printf("too low");
}
}
}
#包括
#包括
#包括
#包括
#熔断器NOWDT
#熔断器
#保险丝名称
#保险丝无保护
#熔断器NOCPD
#保险丝断电
#伊索熔丝
#熔断器
#保险丝NOLVP
#熔丝NODEBUG
#保险丝NOWRT
#保险丝BORV40
#保留保险丝
#内部保险丝
#使用延迟(时钟=8M)
#使用rs232(波特率=9600,奇偶校验=N,xmit=PIN_C6,rcv=PIN_C7,位=8)
void main()
{
int-myInput;
int随机;
random=rand()%5;
printf(“键入字符\r\n”);
printf(“%d”,随机);
而(1){
myInput=getc();
printf(“您键入的-%d\r\n”,myInput);
如果(myInput>随机){
printf(“过高\n”);
}
else if(myInput您必须通过调用srand()
函数来初始化rand()
的起始点。
第399页上有一个很好的例子
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main (void){
time_t toc;
int i;
time(&toc);
srand((int)toc);
for(i = 0 ; i != 10 ; i++)
printf("%d\t", rand());
putchar(’\n’);
}
#包括
#包括
#包括
真空总管(真空){
时间(toc);;
int i;
时间(toc);
srand((int)toc);
对于(i=0;i!=10;i++)
printf(“%d\t”,rand());
putchar('\n');
}
您必须通过调用srand()
函数来初始化rand()的起始点。
第399页上有一个很好的例子
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main (void){
time_t toc;
int i;
time(&toc);
srand((int)toc);
for(i = 0 ; i != 10 ; i++)
printf("%d\t", rand());
putchar(’\n’);
}
#包括
#包括
#包括
真空总管(真空){
时间(toc);;
int i;
时间(toc);
srand((int)toc);
对于(i=0;i!=10;i++)
printf(“%d\t”,rand());
putchar('\n');
}
这是否回答了您的问题?您也在使用(模拟)pic.Sostdlib.h
time.h
和stdio.h
都是奇怪的库。如果你需要随机数而不是使用硬件。这能回答你的问题吗?这似乎没有任何pic特定的,因为它是正在使用的标准库(虽然可能存在16位与32位与64位之间的差异)。如果您想了解更多信息,请删除模(%5
)以查看rand()中的实际值。第一个值可能正好可以被5整除。这是否回答了您的问题?您还使用了(模拟)pic.Sostdlib.h
time.h
和stdio.h
都是奇怪的库。如果你需要随机数而不是使用硬件。这能回答你的问题吗?这似乎没有任何pic特定的,因为它是正在使用的标准库(虽然可能存在16位与32位与64位之间的差异)。如果您想了解更多信息,请删除模数(%5
)以查看rand()中的实际值。第一个值可能正好可以被5整除。