% modulo for random, remainder

Pages: 12
i know that % modulo can be used for remainder
also know that % can be used for randomizig numbers

how exactly does % do these?

there must be a couple functions to explain

not meaning

1
2
3
4
5
6
7

if (y %2 = 0)

or

y = rand() % 47 + 1;


the specific way the language goes about doing both

Both works the same way. rand() gives you a big random number between 0 and RAND_MAX. Using % 47 gives you the remainder of dividing the random number with 47, which gives you a number between 0 and 46.
Last edited on
yea where RAND_MAX is an unsigned number
meaning non negative

0 could be number[first]

rand_max could be number[last]

for example

so then it's the in between code i'd be lookin for
Are you asking how the computer calculates the remainder?
how determines random number set
from 1 to 8 or so

remainder good explanation too
Last edited on
When you do rand() % 47 it is calculating the remainder. That's why you get a number in the range 0-46.

I don't know exactly how % is implemented. Is it important to know? It probably depends on the hardware and on the values involved anyway. I guess it's just a few instructions at most.
Last edited on
rand() % 47; gets number from 0 to 47

rand() % 46; gets range 0 - 46

i think it's important to know

the more you know about programming the more you can do

looking at web link above

random shuffle should answer well for me

1
2
3
int y = 0;

y = rand() % 47 + 1;


my example above is range 1 - 47

threw in int y = 0;

Last edited on
rand() % 47; gets number from 0 to 47

That expression will never give you 47 because the remainder is always going to be less than the divisor.
Last edited on
i was able to obtain 47 as a range number with


rand() % 47;

numbers 0 to 47

with

rand() % 47 + 1; even

i was able to obtain 47 as a range number with


rand() % 47;

No you weren't.

47 / 47 = 1 with a remainder of 0.
You'll never get a remainder of 47.

with

rand() % 47 + 1; even

Yes, because you added one to the remainder, so (46 + 1) is 47.


By the way, using remainder introduces bias into your random numbers. Take the time to do it right. (Cut-n-paste!)
http://www.cplusplus.com/faq/beginners/random-numbers/#random_int_in_range
yes i did

i saw it on my screen

i can't argue with my visual sight

by rand

rand() % 47 + 1;

is range 1 to 47

rand() % 47; is range 0 to 47
Last edited on
must have something to do with RAND_MAX

although % is acting as a divisor obtaining a remainder

RAND_MAX is an obtainable range

whatever the hex value by the programming language

early compilers 0x7FFFU unsigned

new language apparently INFINITY

LOL, can't resist.

yes i did

i saw it on my screen

i can't argue with my visual sight

You're a liar.

Prove it happened.
"Yes, because you added one to the remainder, so (46 + 1) is 47."




(46 + 1) should be the same as rand() % 47;

values from 0 to 47 by RAND_MAX



"with"

"rand() % 47 + 1; even"

"Yes, because you added one to the remainder, so (46 + 1) is 47."


divisor is to 47, +1 ups low range by 1

range is now 1 to 47





run this on cshell

till you attain expected range

1 to 47

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{

  int y[8];
  int p;

  srand((unsigned) time(NULL));

  for (p = 0; p < 8; p++)
   {
     y[p] = rand() % 47 + 1;
   }

  for (p = 0; p < 8; p++)
    {
      printf("%d  ", y[p]);
    }

return 0;

}
Last edited on
x % 46 + 1 is not an equivalent expression to x % (46 + 1). It's equivalent to (x % 46) + 1.

x % 46 is a number between 0 and 45 inclusive.
x % 46 + 1 is a number between 1 and 46 inclusive.
x % 47 is a number between 0 and 46 inclusive.
By definition of integer division, the remainder of a dividend and a divisor cannot have a value greater than or equal to the divisor.
dividend = divisor * quotient + remainder
If remainder = divisor then
dividend = divisor * quotient + divisor
dividend = divisor * (quotient + 1)
dividend = divisor * quotient'

1
2
3
4
5
6
#include <iostream>

int main(){
    for (int i = 0; i < 100; i++)
        std::cout << i << ' ' << (i % 46) << ' ' << (i % 46 + 1) << ' ' << (i % 47) << std::endl;
}
is x variable equivalent to rand() function?

x is any integer value you want. For example, that which is returned by rand().
Pages: 12