#What does Modulus do?

67 messages · Page 1 of 1 (latest)

viscid heartBOT
#

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.

latent flower
#

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

knotty anchor
latent flower
knotty anchor
#

So 10 / 3 = 3 and 10 % 3 = 1

#

like this?

latent flower
knotty anchor
knotty anchor
latent flower
knotty anchor
#

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

viscid heartBOT
#
Generating Random Numbers in C++

The <random> header in C++ provides (pseudo-)random number generation (PRNG):

Example: Printing Ten Random Dice Rolls
#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) << ' ';
} ```
Possible Output (will be different each time)
1 1 6 5 2 2 5 5 6 2 ```
Common Generators
plucky estuary
#

there are better tools in C++ to do that

knotty anchor
#

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?

plucky estuary
#

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

knotty anchor
#

im very new to C++ so my class taught us by using rand()

#

so theres a better way to make a random number generator?

plucky estuary
knotty anchor
#

hm

#

i dont know what dev means

#

they havent taught me that

plucky estuary
#

that's the name of the variable

knotty anchor
#

so dev(100) means 100?

plucky estuary
#

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

knotty anchor
#

so dev() is the same as time()?

#

like it works the same?

plucky estuary
#

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

knotty anchor
#

this seems so much better

#

why would they teach me time() and not this other one?

plucky estuary
#

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

knotty anchor
#

maybe thats why

plucky estuary
#

there have been proposals for a std::randint function that would simplify this by a lot, but that never made it into the language

knotty anchor
#

wow

plucky estuary
#

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

plucky estuary
#

it's going to work more or less correct, the numbers just won't be uniformly distributed

knotty anchor
#

dang

plucky estuary
#

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

knotty anchor
#

I kind of get it..?

plucky estuary
#

or 27 % 100 == 27 and 127 % 100 == 100

#

there is no 128 though, so only the first 27 numbers can appear with double likelihood

viscid heartBOT
#

@knotty anchor Has your question been resolved? If so, run !solved :)

knotty anchor
#

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

plucky estuary
#

(◠‿◠✿)

knotty anchor
#

!solved

viscid heartBOT
#

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 Do Not Delete Posts!

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.