• 0

Battleship: Placing Ships


Question

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

4 answers to this question

Recommended Posts

  • 0

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

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.