C++ Guy Posted June 6, 2009 Share Posted June 6, 2009 Is there an easy way to get a random number in a certain range in c++? All i can find is about a 7 line code one. Is there an easy one like this in C? randomNum = get_new_random(1,9); Link to comment Share on other sites More sharing options...
0 Vieira Posted June 6, 2009 Share Posted June 6, 2009 randomNum = std::rand(); This will generate numbers [0,1]. You can then get a custom range doing something like randomNum*max+min Link to comment Share on other sites More sharing options...
0 Lant Posted June 6, 2009 Share Posted June 6, 2009 No there's no easy straightforward way to do so. You either have to use a C way: srand(time(0)); int random = (rand() % max) + min; From a google search you will see that this method has several problems and that there are several other ways of generating a better (more random) sequence of numbers. Or use the even more complicated TR1 random number generators Link to comment Share on other sites More sharing options...
0 shakey_snake Posted June 6, 2009 Share Posted June 6, 2009 Just modulo whatever result you're getting. Link to comment Share on other sites More sharing options...
0 C++ Posted June 6, 2009 Share Posted June 6, 2009 Doppelganger! Link to comment Share on other sites More sharing options...
Question
C++ Guy
Is there an easy way to get a random number in a certain range in c++? All i can find is about a 7 line code one. Is there an easy one like this in C?
Link to comment
Share on other sites
4 answers to this question
Recommended Posts