When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
67 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
it's defined such that a/b*b+a%b == a when a/b is valid
it's the part that gets lost in the rounding of a/b
for integers
So modulus gives me the remainder instead of how many times a number can go into another number?
yes, % is returning the remainder
mhm
Thank you!
also how do I use it when creating a RNG?
wdym?
so im trying to make a random number generator but there was a tutorial on youtube, and they put
srand (time (0));
cout << (rand () % 6);
so im trying to understand modulus being used here
The <random> header in C++ provides (pseudo-)random number generation (PRNG):
#include <random>
#include <iostream>
int main() {
std::random_device dev; // for seeding
std::default_random_engine gen{dev()};
std::uniform_int_distribution<int> dis{1, 6};
for (int i = 0; i < 10; ++i)
std::cout << dis(gen) << ' ';
} ```
1 1 6 5 2 2 5 5 6 2 ```
:cppreference: Pseudo-random number generation :stackoverflow: Generate random numbers using C++11 random library :stackoverflow: Why is the use of rand() considered bad?
using % for this wouldn't be correct anyways, the numbers won't be uniformly distributed
there are better tools in C++ to do that
int main() {
srand (time (0));
for (int i = 0; i < 10; i++)
{
cout << (rand () % 6) + 1 << "\t";
}
} // end of main
this is it completed sorry i forgot the rest of the program
im just trying to understand how modlus works here when making a random number generator
So i get it is getting random numbers between 1 and 6 but what is modulus being used for here?
rand() generates uniformly distributed ints in some range [0, RAND_MAX]
using the remainder operator just maps that onto the range [0, 5]
however, this is biased and not actually uniform, which is why it's a bad implementation
rand() is just trash in pretty much every regard
im very new to C++ so my class taught us by using rand()
so theres a better way to make a random number generator?
yeah, see this
that's the name of the variable
so dev(100) means 100?
dev just has a call operator which generates truly random bits
so calling dev() gives you some seed that will be different each time you run the program
but using it directly is very slow, which is why you seed a PRNG with it and use that in the rest of the code
yeah, the parentheses are a function call here
time() is a poor-man's seed; you would get the same result if you ran the program twice within the same second
std::random_device gets you entropy from more than just the system clock
because it's more verbose I guess; it doesn't "look" that easy even though it's a much better solution
at the end of the day, rand() kinda works and it's just some regular function call; there is much less to explain
maybe thats why
there have been proposals for a std::randint function that would simplify this by a lot, but that never made it into the language
wow
you would just write
std::cout << std::randint(1, 6) << ' ';
then for example
unfortunately, we don't have this; you could make a function like that yourself relatively easily though
so this doesnt work?
it's going to work more or less correct, the numbers just won't be uniformly distributed
dang
in this case you wouldn't notice because the range is too small for the bias to be obvious
consider if rand() hypothetically generated 7 random bits, so you had a uniform range [0, 127]
and you did rand() % 100
the first 27 numbers would be twice as likely then, because 0 % 100 == 0 and 100 % 100 == 0, for example
I kind of get it..?
or 27 % 100 == 27 and 127 % 100 == 100
there is no 128 though, so only the first 27 numbers can appear with double likelihood
@knotty anchor Has your question been resolved? If so, run !solved :)
I kind of get it..?
I think i need to try using these different commands myself to fully understand
@latent flower@plucky estuary Thank you for helping me
(◠‿◠✿)
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
@knotty anchor
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.