Code: Select all
int DecimalNum(int bitnum)
{
randomSeed(millis());
int MAX = pow(2, bitnum);
int mini = pow(2, bitnum - 1);
int randomNum = random(mini, MAX);
return randomNum;
}
Code: Select all
howbig
Code: Select all
int MAX = pow(2, bitnum);
int mini = pow(2, bitnum - 1);
< /code>
theoretisch sollte keine Fehler verursachen.int MAX = pow(2, bitnum);
int mini = pow(2, bitnum - 1) + 1;
< /code>
Und zu meiner Überraschung hat es funktioniert. Ich verstehe jedoch nicht, warum diese Änderung es zum Laufen gebracht hat, und ich bin gespannt darauf, den Grund dafür zu lernen. Deshalb poste ich diese Frage. < /P>
Diese Funktion ist Teil eines Programms namens "The Game of Binary". Hier ist der vollständige Code des Programms (wieder funktioniert es). Ich hoffe, es kann Ihnen helfen, meine Frage zu verstehen. Die Hardware erfordert einen LCD-Bildschirm und zwei Tasten als Pull-up-Widerstandsschalter. Verzeihen Sie mir auch, dass ich keine Kommentare geschrieben habe, aber keine Sorge - das Programm ist sehr einfach. < /P>
Ich hoffe, Sie können mir helfen, herauszufinden, warum dieses unerwartete Verhalten auftritt.#include
#include
#include
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ONE_BTN_PIN = 12;
const int ZERO_BTN_PIN = 13;
int ONE_BTN_State = 0;
int ZERO_BTN_State = 0;
void setup()
{
pinMode(ONE_BTN_PIN, INPUT_PULLUP); pinMode(ZERO_BTN_PIN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
String message ="This is the game of binary.";
lcd.setCursor(0,1); lcd.print("0 & 1 for skip");
for(int i=0; i= 0; i--)
{
binaryString += String((qustion >> i) & 1);
}
return binaryString;
}
String ReadAnswer(int bitnum)
{
String ans = "";
unsigned long startTime = millis();
unsigned long timeLimit = 10000;
for(int i = 16 - bitnum; i < 16; i++)
{
for(;;)
{
int timeLeft = (timeLimit - (millis() - startTime)) / 1000;
lcd.setCursor(14, 0); lcd.print(" ");
lcd.setCursor(16 - String(timeLeft).length(), 0); lcd.print(timeLeft);
ONE_BTN_State = digitalRead(ONE_BTN_PIN); ZERO_BTN_State = digitalRead(ZERO_BTN_PIN);
if(ONE_BTN_State == HIGH && ZERO_BTN_State == HIGH)
break;
}
for(;;)
{
int timeLeft = (timeLimit - (millis() - startTime)) / 1000;
lcd.setCursor(14, 0); lcd.print(" ");
lcd.setCursor(16 - String(timeLeft).length(), 0); lcd.print(timeLeft);
ONE_BTN_State = digitalRead(ONE_BTN_PIN); ZERO_BTN_State = digitalRead(ZERO_BTN_PIN);
if(ONE_BTN_State == LOW && ZERO_BTN_State == HIGH)
{
lcd.setCursor(i, 1); lcd.print("1");
ans += "1";
break;
}
else if(ZERO_BTN_State == LOW && ONE_BTN_State == HIGH)
{
lcd.setCursor(i, 1); lcd.print("0");
ans += "0";
break;
}
if(millis() - startTime > timeLimit)
{
lcd.clear();
lcd.setCursor(0, 1); lcd.print(" LOSE ");
loop();
}
}
}
return ans;
}
void loop()
{
for(int level = 1; level
Der Quellcode von zufällig < /p>
extern "C" {
#include "stdlib.h"
}
void randomSeed(unsigned long seed)
{
if (seed != 0) {
srandom(seed);
}
}
long random(long howbig)
{
if (howbig == 0) {
return 0;
}
return random() % howbig;
}
long random(long howsmall, long howbig)
{
if (howsmall >= howbig) {
return howsmall;
}
long diff = howbig - howsmall;
return random(diff) + howsmall;
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
unsigned int makeWord(unsigned int w) { return w; }
unsigned int makeWord(unsigned char h, unsigned char l) { return (h