PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Zufallsfunktion immer gleich?



The EYE
11-12-2010, 16:57
Hallo!

Ich arbeite gerade an einer "Zufallsfunktion" in C++.
Aufgabe ist, dass nur Einsen und Nullen in einer "zufälligen" Reihenfolge ausgegeben werden sollen. Es soll der prozentuale Anteil der Ausgabe bestimmt werden können (in meinem Programmcode aktuell 50%, also ausgeglichen).

Mein Problem ist noch, dass die Ausgabe immer gleich aussieht (nur wenn ich die prozentuale Verteilung nicht ändere natürlich).

Ich glaube ich müsste noch ctime includen und srand nutzen um eine scheinbare Zufallsfunktion (mit der Zeit gekoppelt) zu bekommen, bräuchte jetzt aber mal eure Hilfe.

Hier der bisherige Code:

#include<iostream>

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;
if(rand()<g)
return 0;
else
return 1;
}

Gruß Max

edit: rand() durch srand(time(0)) zu ersetzen und noch <ctime> einzubinden bringt leider Fehler mit sich.


#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;
if(srand(time(0))<g)
return 0;
else
return 1;
}

peschmae
11-12-2010, 17:23
Vorschlag:


#include<ctime>
#include<cstdlib> // brauche ich schon für rand()
...


int zufall(double p);


// Hauptprogramm
int main()
{
srand(time(0)); // rand seeden mit der aktuellen Zeit in Sekunden; während dieser Sekunde geben Aufrufe des Programms jeweils denselben Output
...

// double, oder sonst ein grosser int-typ für p, weil bei mir RAND_MAX so gross ist, dass RAND_MAX*p einen integer-overflow erzeugt und der Code damit nicht funktioniert
int zufall(double p){
float g=(RAND_MAX*p)/100.0;

The EYE
11-12-2010, 17:30
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:

#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:

#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

sommerfee
12-12-2010, 08:55
Ich habe das srand bei mir jetzt auch ins Hauptprogramm geschrieben. Warum kann ich es nicht in die Funktion mit hineinschreiben?

Du kannst schon, aber:


Deine Funktion zufall() benötigt mehr Rechenzeit, wenn auch noch srand() mit drinsteht
rand() ist darauf ausgelegt, daß es brauchbar verteilte Zufallszahlen liefert, wenn es einmalig mit srand() initialisiert wird. (Stichwort: Güte eines Pseudozufallszahlengenerators) Wenn du also vor jedem Aufruf von rand() ein srand() machst, torpedierst du damit den Verteilungsalgorithmus von rand(), bekommst also schlechtere Zufallszahlen.


Du könnest es aber so in die Funktion zufall() mit übernehmen, daß es nur 1x aufgerufen wird, etwa so:



int zufall(int p)
{
static bool srand_ok = false;
if ( !srand_ok ) { srand_ok = true; srand(time(0)); }

float g=(RAND_MAX*p)/100.0;
if(rand()<g)
return 0;
else
return 1;
}


Liebe Grüße,
Axel

The EYE
12-12-2010, 09:18
Alles klar, super! Ich bedank mich ;-)

Gruß Max

edit:
Jetzt versuche ich gerade ein zweidimensionales Array mt den Werten aus dem Zufallsgenerator zu füttern.
Leider schreibt er dann überall eine 1 hin.


Hier ein Beispielprogramm zur Verdeutlichung:


#include<iostream>
#include<ctime>

using namespace std;


int zufall(int p);


int main()
{
int array[10][10];

// in das array schreiben
srand(time(0));
for (int i = 0; i <= 9; i++)
{
for (int j=0; j <= 9; j++)
{
zufall(50) >> array[i][j];
}
}

// ausgeben des arrays
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j < 10; j++)
{
if (array[i][j] == 0)
{
cout << "0";
}
else
{
cout << "1";
}
}
cout<<"\n";
}
}


// Zufallsfunktion

int zufall(int p){
float g=(RAND_MAX*p)/100.0;
if(rand()<g)
return 0;
else
return 1;
}

Gruß Max

edit 2:
Ein kurzes Umdenken brachte die Lösung!


#include<iostream>
#include<ctime>

using namespace std;


int zufall(int p);


int main()
{
int array[10][10];

// in das array schreiben
srand(time(0));
for (int i = 0; i <= 9; i++)
{
for (int j=0; j <= 9; j++)
{
array[i][j] = zufall(50);
}
}

// ausgeben des arrays
for (int i = 0; i <= 9; i++)
{
for (int j = 0; j < 10; j++)
{
if (array[i][j] == 0)
{
cout << "0";
}
else
{
cout << "1";
}
}
cout<<"\n";
}
}


// Zufallsfunktion

int zufall(int p){
float g=(RAND_MAX*p)/100.0;
if(rand()<g)
return 0;
else
return 1;
}