?
jellyfish schrieb:
Ich verzeifle gerade an nem ähnlichen Problem... ich will zufallswerte zwischen - 0.25 und + 0.25 erstellen...
also ich dachte mir erstmal ich mach am besten zahlen zwischen 0 bis 1/2 erzeuge und dann - 1/4 rechne ...
Also, ich würde das so machen... :
(rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0
So erhältst du zumindest 10000 zufällige Werte zwischen -1/4 und 1/4 (falls du mehr brauchst, die 10000 einfach mit einer anderen Zahl ersetzen).
Beispielprogramm (in c++... sorry, weiß, dass ich im C-Forum bin, aber ich kenn' mich halt besser mit c++ aus und das cout kann man ja auch relativ einfach mit printf ersetzen, genauso wie die entsprechenden header):
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
cout << (rand()%10000) * (1.0/(2*10000.0)) - 1.0/4.0 << endl;
return 0;
}
PS: kann sein, dass es auch einfacher geht, aber das war jetzt mein erster Gedanke, es so zu machen.