Why is rand()%6 biased?

There are two issues with rand() % 6 (the 1+ doesn’t affect either problem). First, as several answers have pointed out, if the low bits of rand() aren’t appropriately uniform, the result of the remainder operator is also not uniform. Second, if the number of distinct values produced by rand() is not a multiple of … Read more

Why is there no std::stou?

The most pat answer would be that the C library has no corresponding “strtou”, and the C++11 string functions are all just thinly veiled wrappers around the C library functions: The std::sto* functions mirror strto*, and the std::to_string functions use sprintf. Edit: As KennyTM points out, both stoi and stol use strtol as the underlying … Read more

When using C headers in C++, should we use functions from std:: or the global namespace?

From the C++11 Standard (emphasis mine): D.5 C standard library headers [depr.c.headers] For compatibility with the C standard library … Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace … Read more

Deleting elements from std::set while iterating

This is implementation dependent: Standard 23.1.2.8: The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements. Maybe you could try this — this is standard conforming: for (auto it = numbers.begin(); it != numbers.end(); ) { … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)