0

I have a vector of strings and it has three colors. My output only gives me one the first color 'red' where i want it to be random. vector<string> colors = {"red", "green", "yellow"}; there is something wrong with this code Variety x= static_cast<Variety>(rand()%3); fruit.insert(make_pair(x,colors[rand()%3])); i tried doing +1 but that just makes it green.

enum class Variety {
    orange,
    pear,
    apple
};

vector<string> colors = {"red", "green", "yellow"};

struct Fruit {
    Variety v; string color; // red, green or orange
};

int main() {
    multimap<Variety,string> fruit;
    bool foundOrange;
    for (auto j = 0; j < (rand() % 100 + 1); ++j) {
        Variety x = static_cast<Variety>(rand() % 3);
        fruit.insert(make_pair(x, colors[rand() % 3]));
    }
    return EXIT_SUCCESS;
}
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
Keegs
  • 9
  • 1
  • 3
  • 1
    Did you call srand first? – user253751 Feb 28 '20 at 17:18
  • please provide a [MRE] – bolov Feb 28 '20 at 17:19
  • Does this answer your question? [All objects made through constructor have the same vectors](https://stackoverflow.com/questions/60216236/all-objects-made-through-constructor-have-the-same-vectors) – Coral Kashri Feb 28 '20 at 17:20
  • i actually did not. i didn't know you had to call srand first to use rand? – Keegs Feb 28 '20 at 17:21
  • Please provide a small example. – Gerhard Stein Feb 28 '20 at 17:24
  • @Dr.-Ing. Gerhard Stein ```enum class Variety {orange, pear, apple};``` ```vector colors = {"red", "green", "yellow"};``` ```struct Fruit{``` ```Variety v;``` ```string color; // red, green or orange``` }; ```int main(){``` ```multimap fruit;``` ```bool foundOrange;``` ```for(auto j = 0; j<(rand() %100 +1);++j){``` ```Variety x= static_cast(rand()%3);``` ```fruit.insert(make_pair(x,colors[rand()%3]));``` – Keegs Feb 28 '20 at 17:35
  • Have you tried with a seed ```srand(time(0)); ``` – Gerhard Stein Feb 28 '20 at 17:45
  • @ Dr.-Ing. Gerhard Stein i just tried that and it does a different color but displays the prompt too many times. – Keegs Feb 28 '20 at 17:49
  • 1
    Side note: [rand considered harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – user4581301 Feb 28 '20 at 17:56
  • @ user4581301 so should i user srand() then? – Keegs Feb 28 '20 at 18:08
  • 2
    `rand` and `srand` are a matched set. `srand` is used to set up the initial state (seed) that will be used by subsequent calls to `rand`. The problem with `rand` is it's a 1970's solution that deals with 1970s problems like hardware measured in Hz and bytes rather than gigaHz and gigabytes. It take shortcuts that Mr. Lavavej pokes great fun at in his presentation. It can barely be considered random under some circumstances. Modern C++'s [`` library](https://en.cppreference.com/w/cpp/header/random) is discussed at length in the presentation. Prefer to use it. – user4581301 Feb 28 '20 at 18:23

1 Answers1

2

The right way of using rand() is to call srand() first. srand() will generate a series of random numbers, and rand() will simply iterate over them. If you don't call srand() you will get the same number every time you'll use 'rand()`.

srand example:

int main() {
    // Call this function once to generate the random numbers using the seed `time(NULL)`
    srand(time(NULL));

    for (size_t i = 0; i < 10; i++) {
        std::cout << rand() % 5 << std::endl; // Go through the random numbers series
    }

    return EXIT_SUCCESS;
}

However, in C++ there is the <random> library, that will make your life way more easier in the task of generate random numbers (and will work really better then using srand & rand).

// Function to generate random numbers between `min` and `max` params
size_t get_random(size_t min, size_t max) {
    // the generator will only be seeded once since it's static (Equivalent to calling `srand` once)
    static std::mt19937 gen(std::random_device{}());
    std::uniform_int_distribution<size_t> dist(min, max);
    return dist(gen); // generate random number
}

Credit for function idea: @TedLyngmo.
See more delails: All objects made through constructor have the same vectors

Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
  • thank you that fixed it! i didnt call srand() before the use of rand() – Keegs Feb 28 '20 at 19:25
  • @Keegs no problem, but pay attention that `srand` & `rand` may not always work, and there are more recommended ways for generating random numbers in C++. – Coral Kashri Feb 28 '20 at 19:27