-1

I am working on a flappy bird clone to help me learn c++ and SFML. I am working on the pipes, and finally got a working version, but for some reason every time I run the code the rand() function gives me the same result every time.

class Pipes
{
    float x;
    int y;

public:
    Pipes();

        
    void draw(RenderWindow& i_window);
    void update();
};

Pipes::Pipes() :
    y(-(rand() % 175) - 450), // every time I run it, this is -458.
    x(200)
{
    std::cout << y;
}

I am trying to get a number between -450 and -625.

As requested, here is an MRE (i still haven't found the solution):

#include <iostream>
#include <cmath>

class Random
{
    int rndm;

public:
    Random();

};

Random::Random() :
    rndm((rand() % 175) - 450)
{
    std::cout << rndm;
}


int main() {
    Random random;
}
  • 1
    Help us help you - share the code and the error – Mureinik Mar 21 '23 at 17:11
  • 1
    Shot in the dark: [srand() — why call it only once?](https://stackoverflow.com/q/7343833/4581301) – user4581301 Mar 21 '23 at 17:13
  • Yes sorry, I posted it too early, and was editing it to add the rest of it. It normally gives me a chance to review before posting. – Schrödingers Capybara Mar 21 '23 at 17:14
  • Side note: It's 2023, do you know where your [Random Library](https://en.cppreference.com/w/cpp/header/random) is? `rand` can be fast, but it's very weakly specified. [This ancient XKCD joke can be a legal implementation](https://xkcd.com/221/) – user4581301 Mar 21 '23 at 17:16
  • @user4581301 I don't even know what that is, as I said, I am just learning this. If you want, I can send you a link to a view only version of my full code. – Schrödingers Capybara Mar 21 '23 at 17:18
  • The added code eliminates the usual `srand()` followed by `rand()` trope, but still you should make sure that there is only ever one path that leads through `srand`. If you do have a case that needs multiple `srand` calls, you're probably better served by a more robust generator. – user4581301 Mar 21 '23 at 17:18
  • Can you please make a minimal reproducible example? The shown code alone works fine. It's likely that you have made a call to `srand` elsewhere in your code that has reset the sequence of pseudo-random numbers being generated. – Wyck Mar 21 '23 at 17:23
  • Counter suggestion: Make a [mre] (a MRE) that demonstrates the problem you're having. We can tell you exactly what went wrong, and probably within a few minutes. But the true beauty of the MRE is it's hard to make a good one without finding the and fixing the problem part way through. Usually if you make a MRE early in the problem-solving process, you're done before you reach the question-asking stage. – user4581301 Mar 21 '23 at 17:23
  • @user4581301 Edited the question to include an MRE – Schrödingers Capybara Mar 21 '23 at 17:41
  • No call to `srand` at all. I strongly recommend reading [documentation for the `rand` function](https://en.cppreference.com/w/cpp/numeric/random/rand) (every function, really) before using it. C++ and guesswork don't mix. – user4581301 Mar 21 '23 at 18:01
  • @user4581301 I have, I speant forever in cplusplus.com and cpprefrence.com – Schrödingers Capybara Mar 22 '23 at 11:18

1 Answers1

2

Try using <random> library

#include <iostream>
#include <random>
int main()
{    
    const int range_from  = 450;
    const int range_to    = 625;
    std::random_device                  rand_dev;
    std::mt19937                        generator(rand_dev());
    std::uniform_int_distribution<int>  distr(range_from, range_to);

    std::cout << -distr(generator) << '\n';
}
Rogmier
  • 70
  • 6
  • Can you remove all but what's absolutely necessary? And can it all be done in one line, in a variable declaration? – Schrödingers Capybara Mar 21 '23 at 17:25
  • A note about this: `std::mt19937 generator(rand_dev());` and `std::uniform_int_distribution distr(range_from, range_to);` can be brutally expensive. Make these variables once and reuse them. – user4581301 Mar 21 '23 at 17:25
  • 2
    @SchrödingersCapybara that's about as minimal as it gets. Once you have the generator set up you can call `-distr(generator)` as many times as you want. – user4581301 Mar 21 '23 at 17:27