random number always same set of values

Hi guys I'm using visual studio so visualc++ compiler I am trying to generate a random number from one to the length of the string but for some reason the random number being generated only seems to be one of 4 possible values 0,1,16,17 I tried running the program multiple times but these letters only get displayed

any reason why this may be the case?

thanks


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
28
29
30

#include <iostream>
#include <Windows.h>

using namespace std;


const char* generateRandomStr(const char* str) {

	int increment = GetTickCount() & lstrlenA(str);
	cout << increment << endl;
	return str + increment;
}


int main(){


	const char *str = "I love to code!!!";

		for(int i = 0; i < 30; i++) {

			const char *str2 = generateRandomStr(str);
			cout << str2 << endl;
			Sleep(100);
	}
	
	cin.get();
}



GetTickCount returns a 32 bit value. Could be anything, more or less. Let's see that.

xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx

Each 'x' is one bit. Could be 0, could be 1.

You are then bitwise ANDing the number with lstrlenA(str), which has the value 17. Let's see that in binary as well:

0000 0000 0000 0000 0000 0000 0001 0001

Let's see what a bitwise AND of those two values looks like:

0000 0000 0000 0000 0000 0000 000x 000x

It's zero everywhere, except in two places. When you bitwise AND two values, the result will be zero in every bit unless both input bits are 1.

x can be 0 or 1, so here are all the possible values of increment:

0001 0001 = 17
0001 0000 = 16
0000 0001 = 1
0000 0000 = 0

So we can clearly see that your random number generator has only four possible output values.

I am trying to generate a random number from one to the length of the string
Your code, as you can see, is not doing anything remotely like that.
Last edited on
thanks repeater



It's zero everywhere, except in two places. When you bitwise AND two values, the result will be zero in every bit unless both input bits are 1.



but how come all the other bits are zero?

lets say when getTickCount() is called it returns a number we then and it with 17 (0001 0001)
so shouldn't getTickCount return a value that is not all zeros(binary value)?
but how come all the other bits are zero?


That how bitwise and works. You line up the two numbers, and you do AND on every bit, individually.

0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

Here's an example.

1111 1111 1111 (bitwise AND with)
0000 0001 0001

equals

0000 0001 0001

getTickCount return a value that is not all zeros(binary value)?
getTickCount doesn't return all zeros. Let's try another example. Let's say that getTickCount returns all ones:

1
2
getTickCount: 1111 1111 1111 1111 1111 1111 1111 1111 1111
seventeen   : 0000 0000 0000 0000 0000 0000 0000 0001 0001


Bitwise AND result: 0000 0000 0000 0000 000 0000 0001 0001
Last edited on
ok now I think I understand getTickCount() returns an int of a certain value than it is anded with (0001 0001) which gives you 4 possible outputs

but the weird thing is in this video

https://www.youtube.com/watch?v=OoEzlGw8DLo

@16:50 he shows the output of his code and it seems to be a random number between 0 and length of the string

try getting something besides tick count. My gut tells me that is probably doing something unexpected.

you could also use the random tools. They do pretty well seeded from time.

Last edited on
His string is "I love to code!"

That has a length of 15.

Fifteen in binary is 0000 1111

1
2
3
4
getTickCount: xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
fifteen   :   0000 0000 0000 0000 0000 0000 0000 1111

Bitwise And:  0000 0000 0000 0000 0000 0000 0000 xxxx


He has clearly chosen a string of length fifteen so that the random number is the last 4 bits from the getTickCount value, which also means the random number will be from zero to fifteen. Any other length of string will not work.

He has done this in a very badly explained way that has confused you. It's also suspiciously not random; the returned values clearly go upwards monotonically before rolling around at 15.

Edit: Bad typing in binary digits
Last edited on
thanks repeater yeah it confused me quite a bit

15 is 1111 1111 in binary? I thought it would be 1111

or 0000 1111?

the first 1 is 1 second 1 is 2 third 1 is 4 fourth 1 is 8,8+4+2+1 = 15?


thanks
15 is indeed 8+4+2+1.
that is 1111
you are not wrong. I am not sure what he was saying either, but you are correct.

so
int increment = GetTickCount() & lstrlenA(str);
should be
int increment = GetTickCount() % lstrlenA(str);
if I'm reading what your objective is correctly.
Oop. Sorry. Yes, 15 is 1111. I was Too keen to make pretty lining up digits! Everything else I said holds.

When you bitwise any number with 1111, you're basically just slicing off all but the last four bits from your big number.

That's what this guy is doing. He is taking the return from GetTickCount, and he is keeping only the last four bits as his random number, giving him a random number from zero to 15.

He either got his code wrong and meant to use % rather than & , as zaphraud says, or he's being deliberately obtuse and picked a string of length 15 so that bitwise ANDing a bigger number with it would always give a value from zero to 15.
Last edited on
thanks guys

yeah the video really should have explained in more detail on what was going on
Topic archived. No new replies allowed.