Click to See Complete Forum and Search --> : Random Numbers
BenTheMan
April 22nd, 2010, 10:36 AM
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
int myVar = 5;
int *myPointer = 0;
myPointer = myVar;
Now myPointer is a pointer to an address, which is confirmed:
cout << myPointer;
gives (and I quote)
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!
TechGnome
April 22nd, 2010, 10:56 AM
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
MrViggy
April 22nd, 2010, 11:04 AM
Well, yes, this 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
TechGnome
April 22nd, 2010, 11:07 AM
Oh, I'm sure it's a "valid" address.... it just may not be useful. ;)
-tg
superbonzo
April 22nd, 2010, 12:11 PM
Suppose (in C++) I do the following
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
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.
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 :confused: ...
BenTheMan
April 22nd, 2010, 01:00 PM
After writing this, I realize it's a bit meandering, so apologies in advance.
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,
for (int i=0; i<50; i++)
{
localint = i;
myPointer = &localint;
cout << "localint lives at: " << myPointer << endl;
localVar( i );
}
Where localVar is
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 :confused: ...
Again, correct. My mistake--I was glancing over it the other day and read something wrong.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.