vendredi 5 août 2022

Best way to fill a vector with random 0 and 1 in C++ [closed]

I'm trying to fill a vector of (UserInputSize) with random 0 and 1. I found so many ways to generate random numbers but I'm not really sure which one is best and functional for my case. It sounds like a silly question but it ended up being more complicated than i anticipated. I tried using rand()%2 or std::generate and std::generate_n but i couldn't really figure it out (I'm new to C++ i tried looking up some doc but didn't manage to make it work).

i tried :

std::vector<int> GridAsList(GridSizeValue*GridSizeValue);
std::generate_n(std::back_inserter(GridAsList), (GridSizeValue*GridSizeValue), rand()%2);

but i get a "'__gen' cannot be used as a function" error which seems to come from the compiler ? I'm not really sure




Aucun commentaire:

Enregistrer un commentaire