C++ rand() Function Question

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
bshivam2001
Posts: 214
Joined: July 27th, 2013, 6:57 am
Location: India

C++ rand() Function Question

Post by bshivam2001 » September 6th, 2013, 7:03 am

I was going through an old project when suddenly I saw this :!: :idea:

Code: Select all

int r = rand() & (95)
and I wonder what could this mean :?: :?: :?: :?: :?: :?: :?: :?: :?: :?: :?:
And I broke my head wondering :lol:
Does anybody know about it :?: :mrgreen:
'If you can't make it good, at least make it look good'

mmmmmm
Posts: 38
Joined: August 12th, 2013, 6:33 am

Re: C++ rand() Function Question

Post by mmmmmm » September 6th, 2013, 8:04 am

It sets r to an random integer (0-94)

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ rand() Function Question

Post by albinopapa » September 6th, 2013, 8:53 am

That is a different way than what I've seen others do. Since it's using the bitwise "and" operator instead of the % modulus operator it would be a mask right? If that's the case I think it would actually be an integer between 1 and 95. The int 95 would be 00000000 01011111, since the far right digit is a 1 it couldn't return 0.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: C++ rand() Function Question

Post by chili » September 6th, 2013, 12:48 pm

Albinopapa, you're half right. It is a bitwise mask, but it can return zero. Think of:

0000 0000 & 0101 1111 = 0000 0000
or
0010 0000 & 0101 1111 = 0000 0000

Basically, this returns numbers from 0 to 31 and 64 to 95; it cannot return 32 to 63. Can you see why? ;)
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ rand() Function Question

Post by albinopapa » September 6th, 2013, 11:48 pm

chili wrote:Albinopapa, you're half right. It is a bitwise mask, but it can return zero. Think of:

0000 0000 & 0101 1111 = 0000 0000
or
0010 0000 & 0101 1111 = 0000 0000

Basically, this returns numbers from 0 to 31 and 64 to 95; it cannot return 32 to 63. Can you see why? ;)
Don't know how to put it in to words, but I do know how the & works, Intermediate lesson 2 I think, right? If the digits are the same (0 & 0 = 0 or 1 & 1 = 1) the result is that digit, and if they are different the result is 0 (0 and 1 is 0). I can't just look at a bin number or dec number and convert off top of my head like you, but can use calc to figure it out.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

clau007
Posts: 15
Joined: July 30th, 2013, 8:36 pm
Location: Italy

Re: C++ rand() Function Question

Post by clau007 » September 8th, 2013, 11:18 am

rand() returns a pseudo-random INT number

between
0000 0000 0000 0000 (ZERO)
and
1111 1111 1111 1111 (2^15 - 1 or 32767)

if you use the bitwise operator & with

0000 0000 0101 1111 (95)

you will obtain a number from 0 to 95 with the numbers from 32 to 63 excluded because of the 6th bit wich will always be zero.

F.I.:

0010 0110 (38)
&
0101 1111 (95) =
-----------------
0000 0110 (6) not 38....

User avatar
bshivam2001
Posts: 214
Joined: July 27th, 2013, 6:57 am
Location: India

Re: C++ rand() Function Question

Post by bshivam2001 » September 9th, 2013, 4:54 pm

Thanks Guys!
I was really confused by the (95)
;)
'If you can't make it good, at least make it look good'

Post Reply