g++ und zufallszahlen
-
hi!
ich habe fuer einen kollegen ein kleines programm geschrieben, dass zufaellig eine reihe von stimmuli generieren soll. unter windows laeuft es ohne probleme, einfach und gut!
unter linux gehts gar nix, weil er als zufallszahl einfach immer nur 0 ausgibt!
kann mir vielleicht jemand sagen, was ich falsch gemacht habe?#include <iostream> #include <fstream> #include <math.h> #include <cstdlib> #include <ctime> #include <string> #include <vector> using namespace std; int main(int argc, char* argv[]){ srand( (unsigned)time( NULL )); int range=5; int anzahl=0, last=0, val=0, nogo=0; string filename; char output[20]; cout << "Enter Number of Numbers to be generated: "; cin >> anzahl; cout << "\n"; cout << "Enter Filename: "; cin >> filename; cout << "\n"; if(anzahl <=0) return -1; vector<int> seq(anzahl); while(true) { for(int i=0; i<anzahl; ++i){ while(true){ val = ((int) range * rand() / (RAND_MAX))+1; if(!((val == 5 && val == last) || val<1 || val>5)) break; } last = val; if(val==5) ++nogo; seq[i]=val; } if(nogo==(int)(anzahl*20)/100) break; else nogo=0; } ofstream outfile(filename.c_str(), ios::out); for(int i=0; i<anzahl; ++i){ if(seq[i]!=5) sprintf(output,"go0%i",seq[i]); else { sprintf(output,"nogo", seq[i]); ++nogo; } outfile << output << "\t"; cout << seq[i] << endl; } return 0; }
(ja, ich weiss... quick and dirty :))
-
ok, sorry, ich habs jetzt mit einem kollegen selber gefixed!
da in dem gesamten ausdruck nur ints sind, fallen alle ergebnisse die kleiner sind als 1 weg! da aber rand()/RAND_MAX immer kleiner als 1 ist, kommt da Null heraus, dann mal range ist auch null und plus 1 mach immer eins