# cool code

• 01-03-2003
face_master
cool code
Look what I found
Code:

```unsigned long rand1( void ) {         static unsigned long R = 0x12345678;                 R = ( R << 1 ) +                  // shift 1 bit to left, and                 ( (                          // generate rightmost bit by                 ( ( R & 0x80000000L ) != 0 )  // XORing leftmost bit with                 ^ ~( ( R & 4L ) != 0 )        // bit 29 (bit 2 for Little Endian).                 ) & 1 );         return R; }```
It generates a random number. I think its pretty cool.
• 01-03-2003
quzah
Quote:

It generates a random number. I think its pretty cool.

Quzah.
• 01-03-2003
rmullen3
_
That does do some weird stuff though. I understand the binary operations going on one by one, but I don't know how they combine to make a random number considering the operations the same every time... or is it...

EDIT: Yeah, testing now I see it isn't very random. It generates the same numbers every time...
• 01-03-2003
rmullen3
_
feh... I didn't even notice the static specifier...

>_<

there's nothing interesting about that code at all, now = /
• 01-03-2003
frenchfry164
There is nothing random about it. It just shifts some bits around. If you put the same number in there twice, the "random" number will be the same. I know this isn't how it works, but think about this. 1101010b is shifted around to 1010101b. Do an XOR with the 2nd bit, it turns out as 1. Shift the 1101010b to 1010101b and do an XOR on the 2nd bit, turns out 1. See? Now this could complement the rand() if you make it do boolean operations on random bits. I don't know what effects that would do, I think I'm going to try it with XOR or maybe AND.

PS: What is the operator for XOR in C++?
• 01-03-2003
rmullen3
_
XOR is ^