
April 15th, 2014, 04:06 PM
#46
Re: Ising Model C++ Metropolis Algorithm
Originally Posted by 2kaud
Code:
if (delta_E <= 0  ((double)rand() / (double)(RAND_MAX)) < (double)exp((double)delta_E))
the if condition is a logical or. If either or both of the seperate conditons (left and right of the ) is true then the if condition is true.
The first condition is easy. For the second, (double) means cast (convert) the result of what follows to be of type double. As rand() and RAND_MAX are both integers, they are first cast to double so that the result is of type double because in c/c++ an integer divided by an integer gives an integer (3/4 = 0). So the expression to the left of < gives a number of type double in the range 0 to 1 (for the probability required). The expression to the right of the < is what you stated was needed. The (double) casts are needed again to make sure that the types are as required.
See http://msdn.microsoft.com/enus/libr...=vs.60%29.aspx
This would mean that the site (x, y) would be flipped, without doubt. If delta_E < 0, flip. If delta_E > 0, it means that the exponential is between 0 and 1, so flip. I need the flipping process to be such:
1. If delta_E < 0 flip.
2. Otherwise, flip with probability exp (delta_E). If w = 0.7 = 70%, this means that if there are 10 sites with the same value of w, only 7 out of 10 times will site be flipped.

April 15th, 2014, 04:14 PM
#47
Re: Ising Model C++ Metropolis Algorithm
This would mean that the site (x, y) would be flipped, without doubt
No
If delta_E <= 0 then flip
if delta_E > 0 then flip if exp(delta_E) > random_number_0_to_1
When evaluating logical or conditions, the left hand expression is evaluated first. If this is true then the right hand expression is not evaluated. The right hand expression is only evaluated if the left hand expression is evaluated to false. if delta_E is <= 0 then the left condition is true and the right is not evaluated. The right expression is only evaluated if the left is false.
See http://msdn.microsoft.com/enus/library/z68fx2f1.aspx
Last edited by 2kaud; April 15th, 2014 at 04:21 PM.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C, C++ Compiler: Microsoft VS2017.2

April 15th, 2014, 04:16 PM
#48
Re: Ising Model C++ Metropolis Algorithm
Originally Posted by 2kaud
No
If delta_E <= 0 then flip
if delta_E > 0 then flip if exp(delta_E) > random_number_0_to_1
Ah I see, I missed out the > random_number_0_to_1. Ok this makes sense. If exp(delta_E) is between 0 and 1, the chances of it being bigger than a number between 0 and 1 is also 0 and 1. This will work!
Tags for this Thread
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
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
