cool code

This is a discussion on cool code within the C++ Programming forums, part of the General Programming Boards category; Look what I found Code: unsigned long rand1( void ) { static unsigned long R = 0x12345678; R = ( ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    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.
    Please direct all complaints regarding this post to the nearest brick wall Have a nice day.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It generates a random number. I think its pretty cool.
    There's nothing random about it.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    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...

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    feh... I didn't even notice the static specifier...

    >_<

    there's nothing interesting about that code at all, now = /

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    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++?

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    XOR is ^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  4. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21