Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 10:29

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



C++ Randomizer

Discussion on C++ Randomizer within the C/C++ forum part of the Coders Den category.

Closed Thread
 
Old   #1
 
elite*gold: 0
Join Date: Jun 2011
Posts: 265
Received Thanks: 50
Angry C++ Randomizer

Hallo,

Ich versuche seit einigen Stunden verzweifelt nach einem Weg, Zufallszahlen in C++ zu generieren.( z.b. zwischen 123 - 3667 ) Hab tausende Threads durch gelesen und es auch schon mit dem neuen Header
Code:
#include <random>
probiert. Also entweder setz ich es falsch um oder die Leute in den diversen Foren haben einfach kein Plan.

Mit rand() und dem ganzen Scheiß funktioniert es nicht.

Code daweil:
Code:
//Spawn von Powerup
			std::srand( ( unsigned ) std::time( NULL ) );
			if( SpawnTimerPowerup.getElapsedTime() > sf::seconds( 20 + ( std::rand() % ( 45 - 20 + 1 ) ) ) ) //Will Zufallszahl(en) zwischen 20 - 45
			{
				//Powerupsprite erstellen
				sf::Sprite *PSprite;
				PSprite = new sf::Sprite;
				PSprite->setTexture( TPowerup );
				std::srand( ( unsigned ) std::time( NULL ) );
				PSprite->setPosition( 20 + ( std::rand() % ( 770 - 20 + 1 ) ), -50 ); //Will Zufallszahl(en) zwischen 20 - 770

				//PSprite in Liste hinzugefügt
				PowerupListe.push_back( *PSprite );

				//Timer reseten
				SpawnTimerPowerup.restart();
			}
			//
Sorry für meine Wortwahl oben

Mfg,
Kosic
Kosic is offline  
Old 01/30/2013, 03:12   #2
 
Raz9r's Avatar
 
elite*gold: 297
Join Date: Dec 2010
Posts: 1,129
Received Thanks: 1,687
Code:
#include <random>

// precondition: min < max
template <typename E = std::default_random_engine>
int random_int(const int min, const int max) {
	std::random_device rd;
	E gen(rd());
	return std::uniform_int_distribution<>(min, max)(gen);
}
Für dein Beispiel also: random_int(123, 3667) für eine Range von [123, 3667].
Das template-Parameter E ist hierbei die RandomEngine, ich selbst bevorzuge mt19937 - das liefert bei nicht zu langsamen Tempo auch für größere Ranges gute zufällige Zahlen.
Raz9r is offline  
Thanks
1 User
Old 01/30/2013, 14:32   #3
 
elite*gold: 0
Join Date: Jun 2011
Posts: 265
Received Thanks: 50
Danke __underScore !

#closerequest
Kosic is offline  
Old 01/30/2013, 20:07   #4


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
#closed
MrSm!th is offline  
Thanks
1 User
Closed Thread


Similar Threads Similar Threads
DOTA 2 RANDOMIZER EVENT
04/24/2012 - League of Legends Trading - 7 Replies
Hello! Introduction I received 20 DOTA 2 Keys today. I already have Dota2 and my friends too, but i enjoy playing league of legends much more atm. So i start a event where you have the chance to win one or even more. Here are the rules :
IP Randomizer
06/29/2011 - Say Hello - 1 Replies
Hello all!! I do consulting work for small online businesses, but have a problem with accessing more than 1 Amazon.co.uk/.com/.ca seller accounts because Amazon will suspect there are two accounts associated with one seller. How do achieve complete anonymity in regards to Amazon.com's servers? Thank in advance! A.D.
Is there any free IP randomizer out there?
07/17/2010 - S4 League - 9 Replies
I have a feeling that I am being tracked down through my IP, and I don't want to mess around with the cmd to change it manually, in case i mess it up again and unable to connect to the internet... So is there any IP randomizer i can use?



All times are GMT +1. The time now is 10:30.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.