CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Random Numbers

  1. #1
    Join Date
    Feb 2009
    Posts
    7

    Random Numbers

    First I will say, I moderate a physics discussion forum, and we always have crackpots posting crap about quantum mechanics and their new theory of relativity. I can appreciate, then, how random number generation may be a similar lightning rod in a forum such as this.

    However ( )

    I had this idea about getting random numbers, which doesn't work. I have an idea WHY it doesn't work, but I'd like confirmation.

    Suppose (in C++) I do the following

    Code:
    int myVar = 5;
    int *myPointer = 0;
    
    myPointer = myVar;
    Now myPointer is a pointer to an address, which is confirmed:

    Code:
    cout << myPointer;
    gives (and I quote)

    Code:
    0x7fff5fbff7be
    Addresses are assigned (presumably) every time the program is run, and because there are a whole host of background processes running, I would EXPECT that this would give me a random place at some point in the stack (is that the right word?).

    But this doesn't happen. When I run the program again, it gives me the same address. Now, a pragmatist would give up and say ``use boost/random'', which is good, but I'm a physicist which means these kind of things keep me up at night.

    Now that I've mentioned boost/random, I have an additional question, which makes me think that my idea for generating random numbers isn't wholly wrong: the boost random number generator (as near as I can tell) returns a random integer between 1 and 6. So either they're overly concerned with rolling dice, or they're doing something with hexadecimal numbers, which (to me) suggests the memory addresses above.

    Thanks in advance!

  2. #2
    Join Date
    Dec 2007
    Posts
    234

    Re: Random Numbers

    I'm sure at the quantum level, there's a question in there somewhere, but as someone who has a much simpler mind, I don't see it.

    Are you looking for why the address was the same? Or something else?

    -tg

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Random Numbers

    Well, yes, this code:
    Code:
    int myVar = 5;
    int *myPointer = 0;
    
    myPointer = myVar;
    Well, yes, I would expect you to get the same number. Forget the fact that 'myPointer' is a pointer, you're assigning the integer 5 to it. It's no different then if 'myPointer' were an int. You're just pointing to an invalid address.

    Viggy

  4. #4
    Join Date
    Dec 2007
    Posts
    234

    Re: Random Numbers

    Oh, I'm sure it's a "valid" address.... it just may not be useful.

    -tg

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Random Numbers

    Quote Originally Posted by BenTheMan View Post
    Suppose (in C++) I do the following

    Code:
    int myVar = 5;
    int *myPointer = 0;
    
    myPointer = myVar;
    Addresses are assigned (presumably) every time the program is run
    that's neither a valid C++ program nor a valid fragment of it. I suppose you mean

    Code:
    int main()
    {
    	int 	myVar = 5;
    	int* 	myPointer = &myVar;
    }
    now, there are no guarantees deducible from the C++ standard reguarding the specific binary value of MyPointer; it's compiler/os dependent. For example, on Windows (but I suppose on most modern general purpose OSes) each process has its own virtual address space and to each thread is assigned a stack of fixed length (unless specified otherwise) reserved from there. Google about virtual address space/process isolation and so on .. for details.

    Quote Originally Posted by BenTheMan View Post
    the boost random number generator (as near as I can tell) returns a random integer between 1 and 6.
    what ? the boost random number library is a general library certainly not limited to a unformly distributed integer between 1 and 6 ...

  6. #6
    Join Date
    Feb 2009
    Posts
    7

    Re: Random Numbers

    After writing this, I realize it's a bit meandering, so apologies in advance.

    Quote Originally Posted by superbonzo View Post
    Code:
    int main()
    {
    	int 	myVar = 5;
    	int* 	myPointer = &myVar;
    }
    Ahh ****. Yes, you're right. I just got back from lunch and was going to change it.

    now, there are no guarantees deducible from the C++ standard reguarding the specific binary value of MyPointer; it's compiler/os dependent. For example, on Windows (but I suppose on most modern general purpose OSes) each process has its own virtual address space and to each thread is assigned a stack of fixed length (unless specified otherwise) reserved from there. Google about virtual address space/process isolation and so on .. for details.
    I'm a bit confused about what this means. It seems like you're saying that there exists a little place on my RAM that is dedicated to storing local variables that are created in some program. This would imply that this little box of RAM is more or less off limits to everything else going on in the background.

    But if that's the case, why should the address of myVar be the same every time? I mean, suppose I put the (correct) code snippet in some function, then call the function repeatedly. What I find is that the function returns the same memory address every time. So, for example,

    Code:
    for (int i=0; i<50; i++) 
    {
    	        localint = i;
                    myPointer = &localint;
                    cout << "localint lives at: " << myPointer << endl;
    		localVar( i );
    }
    Where localVar is

    Code:
    void localVar( int i )
    {
    	
    	int myVar = i;
    	int *myPointer = &myVar;
    	
    	cout << "myPointer  = " << myPointer  << endl << endl;
    	
    }
    localint is always living at the same place, and the thing that localVar returns is always living at the same place.

    Hmmm, I'm forming a picture of what's happening, so please correct me if I'm wrong. The local variable bin is a string of slots in memory. When I declare a variable, the compiler assigns it a slot in memory, whether I initialize it or not. The compiler sticks the memory in the first slot available. Thus, if I declare a variable in main at the top of the program, the memory will always be in the same place. But really if I declare a variable ANYwhere in my program, the memory is always in the same place, so long as I don't change anything...if I were to add a bunch of variable declarations in front of the ``random'' number, I would just change the address of that variable, but nonetheless the address would be the same until I changed the program again.

    what ? the boost random number library is a general library certainly not limited to a unformly distributed integer between 1 and 6 ...
    Again, correct. My mistake--I was glancing over it the other day and read something wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured