rand() produces 1 number too high? - c++

It is my understanding that rand() % a + b will return numbers between a and b including both a and b. I must be misunderstanding this though, because when I run the code below int r will return 2, 3, or 4. I, of course, am expecting it to return 2 or 3. I'm calling srand(time(NULL)); in main and I'm using
#include <time.h> and #include <stdlib.h>
Am I missing something?
int r = (rand() % 3) + 2;
if (r ==2)
g_fan.draw(r); // skin == 2
else
g_fan.draw(1 + r); //skin == 4

It is my understanding that rand() % a + b will return numbers between a and b including both a and b.
No. It will result in a number between b and (a+b-1), both including.
Range of values of rand() % a is 0 and a-1, both including.
Hence, the range of values of rand() % a + b is b and (a-1+b), both including.
To get random values between a and b, both including, use:
auto interval = (b-a+1);
auto result = a + rand() % interval;

let num be any number you get by calling rand() and if you do % with 3 , there is a possibility of getting one of these number 0, 1, 2.
therefore you are getting 2 ,3, 4 for :
int r = (rand() % 3) + 2;

int r = (rand() % 3) + 2;
The rand() % 3 will return a number between 0 and 2. When you add t2 to each number, that means it will return 2 to 4. The rand() % afunction in general returns a value form 0 to a - 1. When you do rand() % a + b, then the resulting value will range from 0 + b to a - 1 + b.
To get a value between 2 and 3, use:
int r = (rand() % 2) + 2;
The folloing rand() function gives a number from 0 to 2 - 1, which is 1. When you add 2 to each number, you get a range of 0 + 2, which is 2, to 2 - 1 + 2, which is 3.

Related

how can I make branchless number cycle?

I need a branchless number cycle code.
like this:
int i = 0;
i = (i + 1) % 4 //1
i = (i + 1) % 4 //2
i = (i + 1) % 4 //3
i = (i + 1) % 4 //0
i = (i + 1) % 4 //1
...
But it should work in the reverse order of the code above. (3 > 2 > 1 > 0 > 3 > ...)
I first tried "i = (i - 1) % 4".
But this worked differently than I wanted. (-1 > -2 > -3 > 0 > -1 > ...)
However, if I use the method of adding 4 when i is negative, this code is no longer branchless.
How can I implement the functionality which I want (without additional variables or arrays)?
(This article has been translated by Google Translate.)
The error happens because in C89 the remainder of negative numbers was underspecified and from C99, negative % positive will result in a negative number which is unlike in some programming languages such as Python, where (-1) % 4 would indeed result in 3.
But it is easy to circumvent. When you subtract 1, it is the same as adding -1. Since 0 - 1 will get to -1, we would have a negative remainder. To stay positive, instead of adding the negative -1 we can add a positive number that's congruent to -1 (mod m). The smallest positive such number is m - 1 for an m > 1. Therefore we can use:
#define MODULUS 4 // or any other moduli > 1
int i = 0;
i = (i + (MODULUS - 1)) % MODULUS; //3
i = (i + (MODULUS - 1)) % MODULUS; //2
i = (i + (MODULUS - 1)) % MODULUS; //1
i = (i + (MODULUS - 1)) % MODULUS; //0
i = (i + (MODULUS - 1)) % MODULUS; //3
You need to change your expression a bit. It should be:
i = ( i + 3 ) % 4;
In general, if you want a number in range [0,N-1] with such cycle then the equation should be:
i = (i + (N - 1)) % N;
You can see it working here(manually several times) and here (in loop):
int main()
{
int i = 0;
i = ( i + 3 ) % 4; //3
i = ( i + 3 ) % 4; //2
i = ( i + 3 ) % 4; //1
i = ( i + 3 ) % 4; //0
i = ( i + 3 ) % 4; //3
i = ( i + 3 ) % 4; //2
i = ( i + 3 ) % 4; //1
i = ( i + 3 ) % 4; //0
return 0;
}
You can use unsigned arithmetic, then the numbers won't become negative.
unsigned i = 0;
i = (i - 1) % 4 //3
i = (i - 1) % 4 //2
i = (i - 1) % 4 //1
i = (i - 1) % 4 //0
i = (i - 1) % 4 //3
Also, maybe more intuitive, you can use bitwise operations to implement a 2-bit counter.
unsigned i = 0;
i = (i - 1) & 3;
i = (i - 1) & 3;
i = (i - 1) & 3;
i = (i - 1) & 3;
i = (i - 1) & 3;
On machine code level, this is identical to the code above.
There are two cases:
Case 1: The modulo is a power of 2
In this case, you can simply use unsigned arithmetic with a bit mask:
unsigned i = ...;
i = (i - 1) & (modulo - 1);
When i = 0, subtracting 1 will yield a value with all bits set in unsigned arithmetic, and the mask operation & will yield the value modulo - 1.
Case 2: The modulo is not a power of 2
In this case, no fancy bit tricks work. You can only avoid going negative:
i = (i + modulo - 1) % modulo;

Decreasing Loop Interval by 1 in C/C++

Let's say I have 15 elements. I want to group them such a way that:
group1 = 1 - 5
group2 = 6 - 9
group3 = 10 - 12
group4 = 13 - 14
group5 = 15
This way I'll get elements in each group as below:
group1 = 5
group2 = 4
group3 = 3
group4 = 2
group5 = 1
As you can see loop interval is decreasing.
I took 15 just for an example. In actual programme it's user driven parameter which can be anything (hopefully few thousand).
Now what I'm looking for is:
Whatever is in group1 should have variable "loop" value 0, group2 should have 1, group3 should have 2 and so on... "loop" is an int variable which is being used to calculate some other stuff.
Let's put in other words too
I have an int variable called "loop". I want to assign value to it such a way that:
First n frames loop value 0 next (n -1) frames loop value 1 then next (n - 2) frames loop value 2 all the way to loop value (n - 1)
Let's say I have 15 frames on my timeline.
So n will be 5 ====>>>>> (5 + 4 + 3 + 2 + 1 = 15; as interval is decreasing by 1)
then
first 5 frames(1 - 5) loop is 0 then next 4 frames(6 - 9) loop is 1 then next 3 frames(10 - 12) loop is 2 then next 2 frames(13 - 14) loop is 3 and for last frame(15) loop is 4.
frames "loop" value
1 - 5 => 0
6 - 9 => 1
10 - 12 => 2
13 - 14 => 3
15 => 4
I've tried with modulo(%). But the issue is on frame 12 loop is 2 so (12 % (5 - 2)) remainder is 0 so it increments loop value.
The following lines are sample code which is running inside a solver. #loop is by default 0 and #Frame is current processing frame number.
int loopint = 5 - #loop;
if (#Frame % loopint == 0)
#loop += 1;
If I understand this correctly, then
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
for(int i = 1; i <= n; ++i) {
printf("%d: %f\n", i, ceil((sqrt(8 * (n - i + 1) + 1) - 1) / 2));
}
}
is an implementation in C.
The math behind this is as follows: The 1 + 2 + 3 + 4 + 5 you have there is a Gauß sum, which has a closed form S = n * (n + 1) / 2 for n terms. Solving this for n, we get
n = (sqrt(8 * S + 1) - 1) / 2
Rounding this upward would give us the solution if you wanted the short stretches at the beginning, that is to say 1, 2, 2, 3, 3, 3, ...
Since you want the stretches to become progressively shorter, we have to invert the order, so S becomes (n - S + 1). Therefore the formula up there.
EDIT: Note that unless the number of elements in your data set fits the n * (n+1) / 2 pattern precisely, you will have shorter stretches either at the beginning or in the end. This implementation places the irregular stretch at the beginning. If you want them at the end,
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
int n2 = (int) ceil((sqrt(8 * n + 1) - 1) / 2);
int upper = n2 * (n2 + 1) / 2;
for(int i = 1; i <= n; ++i) {
printf("%d: %f\n", i, n2 - ceil((sqrt(8 * (upper - i + 1) + 1) - 1) / 2));
}
}
does it. This calculates the next such number beyond your element count, then calculates the numbers you would have if you had that many elements.

How does Rand work when starting at a number(36) with a max of 75

I am creating a random number generator and whenever I run the current code block I always get a few numbers that go over the intended limit. The way it works is that I have a set of numbers that are randomly generated that go from 36-75, With adjusted numbers that are 5 higher and 5 lower than the original number. For example I will end up with numbers above 75. The highest being 105.
Here is just one of the 6 numbers.
//Displays picks for Number 4
pick4 = (rand() % 75) + 36;
if (pick4 == pick3)
pick4 = (rand() % 75) + 36;
if (pick4 + 5 < 75 + 1)
{
if (pick4 - 5 > 0)
{
adjHighPick4 = pick4 + 5;
adjLowPick4 = pick4 - 5;
}
}
When you want to get a random integer from A to B, you just need a random integer from 0 to B-A, to which you add A. So, instead of rand() % 75 + 36, you should write rand() % 39 + 36 (A=36, B=75, B-A=39)

C/C++ unsigned integer overflow

i'm reading an article about integer security .
here's the link:
http://ptgmedia.pearsoncmg.com/images/0321335724/samplechapter/seacord_ch05.pdf
In page 166,there is said:
A computation involving unsigned operands can never overflow,because a
result that cannot be represented by the resulting unsigned integer
type is reduced modulo to the number that is one greater than the
largest value that can be represented by the resulting type.
What does it mean? appreciate for reply.
It means the value "wraps around".
UINT_MAX + 1 == 0
UINT_MAX + 2 == 1
UINT_MAX + 3 == 2
.. and so on
As the link says, this is like the modulo operator: http://en.wikipedia.org/wiki/Modulo_operation
No overflow?
"Overflow" here means "producing a value that doesn't fit the operand". Because arithmetic modulo is applied, the value always fits the operand, therefore, no overflow.
In other words, before overflow can actually happen, C++ will already have truncated the value.
Modulo?
Taking a value modulo some other value means to apply a division, and taking the remainder.
For example:
0 % 3 = 0 (0 / 3 = 0, remainder 0)
1 % 3 = 1 (1 / 3 = 0, remainder 1)
2 % 3 = 2 (2 / 3 = 0, remainder 2)
3 % 3 = 0 (3 / 3 = 1, remainder 0)
4 % 3 = 1 (4 / 3 = 1, remainder 1)
5 % 3 = 2 (5 / 3 = 1, remainder 2)
6 % 3 = 0 (6 / 3 = 2, remainder 0)
...
This modulo is applied to results of unsigned-only computations, with the divisor being the maximum value the type can hold. E.g., if the maximum is 2^16=32768, then 32760 + 9 = (32760 + 9) % (32768+1) = 0.
It means that you can't alter the sign of a unsigned calculation, but it can still produce unexpected results. Say we have an 8-bit unsigned value:
uint8_t a = 42;
and we add 240 to that:
a += 240;
it will not fit, so you get 26.
Unsigned math is clearly defined in C and C++, where signed math is technically either undefined or implementation dependent or some other "things that you wouldn't expect may happen" wording (I don't know the exact wording, but the conclusion is that "you shouldn't rely on the behaviour of overflow in signed integer values")
One more example to show unsigned data type wraps around instead of overflow:
unsigned int i = std::numeric_limits<unsigned int>::max(); // (say) 4294967295
Assigning a -ve number to the unsigned is not recommended but for the illustrative purpose, I'm using it below
unsigned int j = -1; // 4294967295 wraps around(uses modulo operation)
unsigned int j = -2; // 4294967294
Visualizing the unsigned (0 to max) range with respect to the modulo of max+1 (where max = 2^n):
Range : 0, 1, 2,......., max-2, max-1, max
.................................................................................
Last-to-First : -(max+1), -max, -(max-1),......., -3, -2, -1
First-to-Last : max+1, max+2, max+3,......., max+max-1, max+max, max+max+1
Modulo Addition Rule: (A + B) % C = (A % C + B % C) % C
[max + max + 1] % (max + 1) = [(max) + (max + 1)] % (max + 1)
= [(max % (max + 1)) + ((max + 1) % (max + 1))] % (max + 1)
= [(max % (max + 1)) + 0] % (max + 1)
= [max] % (max + 1)
= max

C++ get every number separately

I have a range of numbers from 100 to 999. I need to get every number separately of it and check whether it can be divided by 2. For example:
232
2 divided by 2 = 1 = true
3 divided by 2 = 1.5 = false
2 divided by 2 = 1 = true
and so on.
To get the first number all I have to do is to divide the entire number by 100.
int x = 256;
int k = x/100;
so x would hold a value of 2.
Now, is there a way to check those other ones? Because k = x/10; would already be 25.
Try this:
int x = 256;
int i = x / 100; // i is 2
int j = (x % 100) / 10; // j is 5
int k = (x % 10); // k is 6
maybe look into integer division and the modulo.
int k1 = (x / 10) % 10 // "10s"
int k2 = ( x / 100 ) % 10 // "100s"
//etc etc
Use modulo to get the last digit of the number, then divide by ten to discard the last digit.
Repeat while the number is non-zero.
What you need is the modulus operator %. It does a division and returns the reminder.
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
4 % 2 = 0
...
eg. take 232:
int num = 232;
int at_ones_place = num % 10;
int at_tens_place = ( num /10 ) % 10 ;
int at_hundreds_place = (num /100);