Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 |
int generate_RandomInt(int x, int y) { random_device rd; mt19937 e2(rd()); uniform_int_distribution<int> dist(x, y); return dist(e2); } |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Der Code ist sehr sehr langsam. Ich weiß, das klingt verblüffend, aber "random_device()" ist saumäßig langsam und schon wenige Calls davon pro Frame haben unsere Framerate massiv einbrechen lassen, unter 4 FPS. Ich rate stark dazu das device einmalig zu holen und dann immer darauf zuzugreifen. Auch das Erzeugen immer neuer Distributions ist nicht unbedingt schnell und meist auch überflüssig.Um eine Zufallszahl in einem Range zu generieren verwende ich (C++, 2011 standard) folgende Funktionen (hier nur die für Int).
C-/C++-Quelltext
1 mt19937 e2(rd());
Korowai
unregistriert
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <iostream> #include <conio.h> #include <iterator> #include <list> #include <vector> #include <list> #include <stdio.h> #include <cstdlib> #include <ctime> #include <string> #include "Spieler.hpp" #include "Cbasis.hpp" #include "CFoerderung.hpp" #include "CPlanet.hpp" #include "Flugwaechter.hpp" #include "Jaeger.hpp" #include "Schuerfer.hpp" #include "Spieler.hpp" #include "Preisentwicklung.hpp" #include "Markt.hpp" using namespace std; string name; int Anzahl; int Runde=1; int plecs0=500000; int plecs1=50000; int plecs2=50000; int plecs3=50000; int EP1=0; int EP2=0; int EP3=0; int EP0=0; float Inflation=0; float Deflation=0; float Kostenniveau=1; float Kostenstand=1; int Basisnummer0=13; int alteRunde=1; const int Mobile=4; float Preisabfrage(float Kostenniveau); float Deflationsabfrage(float Deflation); float Inflationsabfrage(float Inflation); ... int main () { srand(time(0)); ... Menue(..); ... } |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
void Menue(...) { int Umrechnung; float Marktwertanzeige; float Marktwerte; float Kostenniveau; float Deflation; float Inflation; Preisabfrage(Kostenniveau); Deflationsabfrage(Deflation); Inflationsabfrage(Inflation); Kostenniveau=Preisabfrage(Kostenniveau); Deflation=Deflationsabfrage(Deflation); Inflation=Inflationsabfrage(Inflation); while (true) { string Basisname; if (alteRunde<=Runde) { if (Basisnummer0==13) { Marktwerte= (Kostenniveau+Inflation-Deflation)/90*100; }; if (Basisnummer0==1) { Marktwerte= (Kostenniveau+Inflation-Deflation)/105.0*100; }; if (Basisnummer0==9) { Marktwerte= (Kostenniveau+Inflation-Deflation)/90.0*100; } if (Basisnummer0==21) { Marktwerte= (Kostenniveau+Inflation-Deflation)/95.0*100; } }; Umrechnung=Marktwerte*100; Marktwertanzeige=Umrechnung/100.0; // Anzeige zum Test der Werte cout << "Deflation = " << Deflation << endl; cout << "Inflation = " << Inflation << endl; cout << "Kostenniveau = " << Kostenniveau << endl; cout << "Marktberechnung ohne Planet = " << Kostenniveau+Inflation-Deflation << endl; cout << "Markt1.Wertentwicklung = " << Markt1.Wertentwicklung << endl; ... |
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include "Preisentwicklung.hpp" float Preisabfrage(float Kostenniveau) { int Kostenentwicklung; Kostenentwicklung= rand()%99+1; if (Kostenentwicklung<=20) { Kostenniveau=0.85; } else if ((Kostenentwicklung>20)&&(Kostenentwicklung<=40)) { Kostenniveau=0.95; } else if ((Kostenentwicklung>40)&&(Kostenentwicklung<=60)) { Kostenniveau=1; } else if ((Kostenentwicklung>60)&&(Kostenentwicklung<=80)) { Kostenniveau=1.05; } else if ((Kostenentwicklung>80)&&(Kostenentwicklung<=100)) { Kostenniveau=1.15; } return(Kostenniveau); } float Deflationsabfrage(float Deflation) { int Kostenentwicklung; Kostenentwicklung= rand()%99+1; if (Kostenentwicklung<20) { Deflation=0.3; } else if (Kostenentwicklung>=21&&Kostenentwicklung<75) { Deflation=0.1; } else { Deflation=0.2; } return (Deflation); } float Inflationsabfrage(float Inflation) { int Kostenentwicklung=0; Kostenentwicklung= rand()%99+1; if (Kostenentwicklung <20) { Inflation=0.3; } else if (Kostenentwicklung>=25&&Kostenentwicklung<75) { Inflation=0.1; } else { Inflation=0.2; } return (Inflation); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
float Preisabfrage(float Kostenniveau) { static float MöglicheKosten[] = { 0.85, 0.95, 1.0, 1.05, 1.15 }; //Um die Wahrscheinlichkeit anzupassen, ist es am Einfachsten wenn du ein Wert mehrfach vorkommt. return MöglicheKosten[rand()%5]; //Es werden gleich nur 5 ausgewählt, statt 100. Außerdem sind //die Werte in einer kleinen Tabelle anstatt eine monströsen Verzweigung. Normalerweise würde ich noch die Verwendung von //std::extent<decltype(MöglicheKosten)>::value empfehlen, um die 5 wegzubekommen, die man leicht mal vergessen könnte, bei einer Änderung //mit anzupassen. Aber da du offenbar noch eher am Anfang bist, ist die 5 direkt die einfachste Lösung. } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
int generate_RandomInt(int Min, int Max) { static mt19937 Generator(random_device()); uniform_int_distribution<int> Distribution(Min, Max); return Distribution(Generator); } |
Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von »Spiele Programmierer« (27.08.2015, 22:24)
Administrator
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
In Code::Blocks gibt es dafür unter Build-Options auch eine Checkbox, die man setzen kann. Man kann das natürlich auch über Parameter machen, finde ich aber irgendwie kontraproduktiv, bzw. nur dann sinnvoll, wenn die Option unter Code::Blocks tatsächlich nicht angeboten wird und der Compiler es trotzdem kann. Hier könnte genau das der Fall sein, ich wollte es trotzdem mal anmerken, damit nicht jeder Code::Blocks-User die Option per Parameter angibt.(Kommandozeilenargument: -std=c++11)
Korowai
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
float Preisabfrage(float Kostenniveau) { static float MöglicheKosten[] = { 0.85, 0.95, 1.0, 1.05, 1.15 }; int GewählterIndex = rand() % 5; return MöglicheKosten[GewählterIndex]; } |
Werbeanzeige