Hallo!
Danke für deine schnelle Rückmeldung!
Bei mir klappt es auch mit int p und der compiler meckert auch nicht.
Wieso benötigst du unbedingt <cstdlib>? Oder wieso ich nicht?
Ich habe das srand bei mir jetzt auch ins Hauptprogramm geschrieben. Warum kann ich es nicht in die Funktion mit hineinschreiben?
Zur verdeutlichung hier beide Codes:
nicht funktionierender Code:
Code:
#include<iostream>
#include<ctime>
using namespace std;
// Prototyp Zufallsfunktion
int zufall(int p);
// Hauptprogramm
int main()
{
int z;
for (int i=1; i<=100; i++)
{
z = zufall(50);
cout << z;
}
return 0;
}
// Zufallsfunktion
int zufall(int p){
float g=(RAND_MAX*p)/100.0;
srand(time(0));
if(rand()<g)
return 0;
else
return 1;
}
funktionierender Code:
Code:
#include<iostream>
#include<ctime>
using namespace std;
// Prototyp Zufallsfunktion
int zufall(int p);
// Hauptprogramm
int main()
{
int z;
srand(time(0));
for (int i=1; i<=100; i++)
{
z = zufall(50);
cout << z;
}
return 0;
}
// Zufallsfunktion
int zufall(int p){
float g=(RAND_MAX*p)/100.0;
if(rand()<g)
return 0;
else
return 1;
}
Den Unterschied (Position von srand) habe ich rot markiert.
Gruß Max
Lesezeichen