0

I'm a first semester comp sci student working on an assignment. The assignment is seemingly asking for double random numbers however the professor has only shown how to find integers from a random number generator using rand and srand. Any tips on this? a. Write a function to simulate a single shot. It should use the following declaration:

void shoot(bool &targetAlive, double accuracy);

This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy, then the target is hit and targetAlive should be set to false. You need to generate random numbers.

For example, if Bob is shooting at Charlie, this could be invoked as:

shoot(charlieAlive, 1.0);

Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.

unit5016
  • 29
  • 5
  • See example here: https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution – HolyBlackCat Feb 27 '22 at 16:12
  • 1
    If you are forced to use rand() (which has been replaced in 2011 with a much better library) lookup RAND_MAX – drescherjm Feb 27 '22 at 16:12
  • 1
    [https://stackoverflow.com/questions/13408990/how-to-generate-random-float-number-in-c](https://stackoverflow.com/questions/13408990/how-to-generate-random-float-number-in-c) – drescherjm Feb 27 '22 at 16:15
  • Thank you, this has solved the issue. He does not say we are forced to use rand() but its been the only thing he has shown so I will use this and double check with him. – unit5016 Feb 27 '22 at 16:35
  • The first comment has the replacement – drescherjm Feb 27 '22 at 17:00
  • For a short and competent introduction to pseudo-random number generation in C++11 and beyond, see chapter 24.7 in [the latest C++ programming textbook by Bjarne Stroustrup](https://www.stroustrup.com/programming.html), ISBN 978-0321-992789. For a full exposure, the official “Random Number Generation in C++11” N3551 White Paper, 12 pages only, can be obtained [here](https://isocpp.org/files/papers/n3551.pdf). The paper starts with a short discussion of the historical shortcomings of the **legacy** `rand()` and `srand()` functions. – jpmarinier Feb 27 '22 at 18:08

0 Answers0