-
August 28th, 2008, 08:48 AM
#1
How can this be represented in C++?
Hi,
I need to have a C++ representation of this code, but am struggling to convert it. Could someone please put this in C++ or tell me how it could be done?
do
L1=uniform()
L2=uniform()
M1=2 * L1 -1
M2=2 * L2 -1
S=M1 * M1 + M2 * M2
while S >=1
X=sqrt(-2 * log(S) / S) * M1
Y=sqrt(-2 * log(S) / S) * M2
Thanks in advance.
Last edited by CoolShades; August 29th, 2008 at 06:07 AM.
-
August 28th, 2008, 08:51 AM
#2
Re: How can this be represented in C++?
That's pretty straightforward to convert, except for the uniform() call. I'm not sure what that is. If the purpose is to get a value uniformly distributed between 0 and 1, then using rand()/(double)RAND_MAX should do it.
-
August 28th, 2008, 08:55 AM
#3
Re: How can this be represented in C++?
Yep, that's exactly what they're meant to be.
so I use:
Code:
rand()/(double)RAND_MAX
??
Also, when you say that it's pretty straight forward, does that mean that by just declaring L1&2, M1&2, S, X and Y then the rest can be used in a c++ program as they are? (obviously after entering all the necessary semi-colons!?
Thanks for your response btw.
-
August 28th, 2008, 09:40 AM
#4
Re: How can this be represented in C++?
Yeah, and with appropriate brackets around the loop and whatnot.
-
August 28th, 2008, 09:49 AM
#5
Re: How can this be represented in C++?
Note that the rand and log functions are declared in the standard headers <cstdlib> and <cmath> respectively.
- Alon
-
August 28th, 2008, 10:16 AM
#6
Re: How can this be represented in C++?
Also, being pedantic, this
is a C-style cast. Although you can use them in C++, you should really be using a C++ cast which in this case is static_cast:
Code:
rand()/static_cast<double>(RAND_MAX);
Alternatively, you can initialise a temporary double using RAND_MAX, but it will not be as efficient as a cast (although you probably won't notice the performance overhead).
Code:
rand()/double(RAND_MAX);
-
August 28th, 2008, 10:30 AM
#7
Re: How can this be represented in C++?
 Originally Posted by PredicateNormative
is a C-style cast. Although you can use them in C++, you should really be using a C++ cast which in this case is static_cast:
Aside from clarifying that it's not a dynamic_cast or a reinterpret_cast, is there any practical benefit to using a static_cast over a C-style cast? Does it allow compiler optimizations or something?
-
August 28th, 2008, 10:43 AM
#8
Re: How can this be represented in C++?
 Originally Posted by PredicateNormative
Also, being pedantic, this
is a C-style cast. Although you can use them in C++, you should really be using a C++ cast which in this case is static_cast:
Code:
rand()/static_cast<double>(RAND_MAX);
Alternatively, you can initialise a temporary double using RAND_MAX, but it will not be as efficient as a cast (although you probably won't notice the performance overhead).
Code:
rand()/double(RAND_MAX);
Thanks for the explanations.
But for the last 2 snippets (quoted above), I don't understand how they would then be declared or initialized. Would it be possible for you to kindly give me an example?
Thank you.
Last edited by CoolShades; August 28th, 2008 at 10:44 AM.
Reason: Forgot the last line :)
-
August 28th, 2008, 10:58 AM
#9
Re: How can this be represented in C++?
 Originally Posted by Lindley
Aside from clarifying that it's not a dynamic_cast or a reinterpret_cast, is there any practical benefit to using a static_cast over a C-style cast? Does it allow compiler optimizations or something?
No, but it does make the compiler warning go away 
It's more about prevention. A C-style cast will allow you to cast from anything you like, which though useful when intended, can be dangerous when not intended. C++ casts are more restrictive, they are designed to (where possible and correct to do so) bring casting mistakes to compile time, rather than letting you find out at runtime with a crash or some undefined behaviour that you've made a mistake by casting a mouse into a stone, when you actually meant to just cast it to another colour. The point is, that if you can get into the habit of using the correct cast for the occasion, then you are less likely to make mistakes.
The dynamic_cast does give a nice practical benefit over the C-style cast, in that it will return NULL if a cast between one class object and another cast is not possible. For example the following:
Code:
#include <iostream>
class Base
{
public:
virtual ~Base(){}
};
class Derived : public Base {};
int main()
{
Base base;
Derived derived;
Base* baseObj = &base;
Derived* derivedObj = &derived;
if( dynamic_cast<Base*>(derivedObj) )
std::cout << "Cast from Derived object to Base pointer possible" << std::endl;
else
std::cout << "Cast from Derived object to Base pointer NOT possible" << std::endl;
if( dynamic_cast<Base*>(baseObj) )
std::cout << "Cast from Base object to Base pointer possible" << std::endl;
else
std::cout << "Cast from Base object to Base pointer NOT possible" << std::endl;
if( dynamic_cast<Derived*>(baseObj) )
std::cout << "Cast from Base object to Derived pointer possible" << std::endl;
else
std::cout << "Cast from Base object to Derived pointer NOT possible" << std::endl;
if( dynamic_cast<Derived*>(derivedObj) )
std::cout << "Cast from Derived object to Derived pointer possible" << std::endl;
else
std::cout << "Cast from Derived object to Derived pointer NOT possible" << std::endl;
}
gives the output:
Code:
Cast from Derived object to Base pointer possible
Cast from Base object to Base pointer possible
Cast from Base object to Derived pointer NOT possible
Cast from Derived object to Derived pointer possible
-
August 28th, 2008, 11:24 AM
#10
Re: How can this be represented in C++?
 Originally Posted by CoolShades
Thanks for the explanations.
But for the last 2 snippets (quoted above), I don't understand how they would then be declared or initialized. Would it be possible for you to kindly give me an example?
Thank you.
You should read some the documentation on rand and srand to understand what they are doing and how they work and to see where RAND_MAX comes from:
http://www.cplusplus.com/reference/c...dlib/rand.html
http://www.cplusplus.com/reference/c...lib/srand.html
but effectively:
Code:
rand()/(double)RAND_MAX;
and
Code:
rand()/static_cast<double>(RAND_MAX);
and
Code:
rand()/double(RAND_MAX);
all do the same thing, they all will result in a value of type double between 0 and 1, to make it as random as possible though, you should re-seed srand before each call to rand(), the most common way is to use the system clock.
The usage of the above are:
Code:
double val = rand()/(double)RAND_MAX;
or
Code:
double val = rand()/static_cast<double>(RAND_MAX);
or
Code:
double val = rand()/double(RAND_MAX);
I would personally suggest using the one with the static_cast.
-
August 28th, 2008, 11:49 AM
#11
Re: How can this be represented in C++?
>> to make it as random as possible though, you should re-seed srand before each call to rand()
The will destroy the "randomness". Even so, the described methods are not the best for a uniform distribution.
http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
gg
-
August 28th, 2008, 11:58 AM
#12
Re: How can this be represented in C++?
To emphasize: Do not call srand() more than once in a program! I'd suggest not calling it at all until you're done debugging.
Certainly do not call it more than once per second, if you're going to use time(NULL) as its argument, because then you'll re-seed it to the same place twice, and thus get the same rand() value at least twice, every time.
-
August 28th, 2008, 12:07 PM
#13
Re: How can this be represented in C++?
 Originally Posted by Codeplug
>> to make it as random as possible though, you should re-seed srand before each call to rand()
The will destroy the "randomness". Even so, the described methods are not the best for a uniform distribution.
Whoops, I misread the documentation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|