zusammenfassend lässt sich sagen:
so funktioniert die zufallsfunktion sehr gut. ich denke die art der umsetzung ist auch recht sauber. ein test mit folgendem programm:Code:/*Funktion, die eine Zufallszahl zwischen min und max liefert*/ int Zufall(int min, int max) { static uint16_t zahl=0; uint8_t adc; int n; for(n=0;n<16;n++) { adc=(ReadADC(WHEEL_LEFT,0)+ReadADC(WHEEL_RIGHT,0)+ReadADC(IR_LEFT,0)+ReadADC(IR_RIGHT,0)+ReadADC(BATTERIE,0))/5; adc&=0x01; adc=adc<<n; zahl=zahl+adc; } srand(zahl); return min+(rand()%(max-min+1)); }
lieferte bei mir dieses ergebnis:Code:int main(void) { Init(); EncoderInit(); while(1) { if (Taster()!=0) { Msleep(1000); while(1) { int i=0; int n; SerPrint("Zufallszahlen zw. 0 und 1"); SerPrint("\n\r"); for (n=0;n<10;n++) { i=Zufall(0,1); PrintInt(i); SerPrint(" - "); Msleep(500); } Msleep(1500); SerPrint("\n\r"); SerPrint("Zufallszahlen zw. 0 und 50"); SerPrint("\n\r"); for (n=0;n<10;n++) { i=Zufall(0,50); PrintInt(i); SerPrint(" - "); Msleep(500); } SerPrint("\n\r"); SerPrint("Zufallszahlen zw. 80 und 500"); SerPrint("\n\r"); Msleep(1500); for (n=0;n<10;n++) { i=Zufall(80,500); PrintInt(i); SerPrint(" - "); Msleep(500); } SerPrint("\n\r"); SerPrint("Zufallszahlen zw. 0 und 60000"); SerPrint("\n\r"); Msleep(1500); for (n=0;n<10;n++) { i=Zufall(0,60000); PrintInt(i); SerPrint(" - "); Msleep(500); } SerPrint("\n\r"); SerPrint("Zufallszahlen zw. 0 und 50"); SerPrint("\n\r"); Msleep(1500); for (n=0;n<10;n++) { i=Zufall(0,50); PrintInt(i); SerPrint(" - "); Msleep(500); } Msleep(1500); } } else Blinken('S'); } while (1); return 0; }
daran sieht man, dass die zahlen doch sehr zufällig vorkommen und vor allem, dass 2 aufrufe der funktion mit den gleichen min- und maxwerten nicht zu den gleichen (und damit zu keinen pseudozufallszahlen) führen.Zufallszahlen zw. 0 und 1
0 - 1 - 1 - 0 - 0 - 0 - 1 - 1 - 0 - 0 -
Zufallszahlen zw. 0 und 50
22 - 22 - 1 - 17 - 12 - 28 - 50 - 33 - 14 - 28 -
Zufallszahlen zw. 80 und 500
332 - 141 - 201 - 246 - 427 - 361 - 365 - 174 - 371 - 155 -
Zufallszahlen zw. 0 und 60000
656 - 4886 - 947 - 4516 - 577 - 1505 - 2142 - 4125 - 3851 - 90 -
Zufallszahlen zw. 0 und 50
50 - 24 - 26 - 16 - 37 - 8 - 24 - 5 - 4 - 3 -
Zufallszahlen zw. 0 und 1
0 - 1 - 1 - 1 - 1 - 1 - 0 - 1 - 0 - 0 -
Zufallszahlen zw. 0 und 50
11 - 45 - 19 - 11 - 39 - 29 - 39 - 28 - 14 - 23 -
Zufallszahlen zw. 80 und 500
216 - 303 - 348 - 488 - 244 - 320 - 250 - 339 - 431 - 108 -
Zufallszahlen zw. 0 und 60000
933 - 1135 - 2625 - 2625 - 2625 - 2005 - 491 - 2289 - 0967 - 3539 -
Zufallszahlen zw. 0 und 50
38 - 33 - 35 - 4 - 42 - 23 - 11 - 18 - 40 - 19 -
einzig verwunderliche ist, dass bei der zufallszahl zw. 0 und 60000 kein wert größer ist als 5000. leigt das vielleicht am wertebereich des int?!
Lesezeichen