问题
Thank you very much for your help for coding my model,
If you do not mind please, I want to ask you about some interpretations in the coding I am sorry I am not expert in mathematics
to move
ask turtles with [gender = "male" ]
[ if ( random-float 1) <= 0.025]
why it is <= and what is the interpretation of this code,
and for the percentage
ask turtles
[ if random 100 <= 50
[become-fat]]
the same question why <= if we always say 50 % in the group will be fat why we put this sign???
and what is the different between random and random float
sorry for disturbance
回答1:
The difference between the two primitives is that:
- random gives you only integer numbers, e.g.: 0, 1, 2, 3, etc.
- random-float gives you floating point numbers, e.g.: 0.0, 0.125, 0.528476587245, 3.66, etc.
Both can be used to make things happen probabilistically in NetLogo. I'll start with the use of random
, which is slightly easier to understand.
Using random
As stated in the documentation, if you pass a positive number to random
, it will give you a number that is greater or equal to 0, but strictly less than that number.
For example, random 2
will always give you either 0 or 1. You could use that to simulate the flipping of a coin:
ifelse random 2 = 0 [ print "heads" ] [ print "tail" ].
That will print "heads"
50% of the time (when random 2
gives you 0), and "tail"
50% of the time (when random 2
gives you 1).
Now it's easy to generalize this to probabilities expressed in percentages by using random 100
instead of random 2
. I'll use an example with 50%, but it could very well be 25%, 80% or even 1% or 100%.
Now since random 100
gives you a number between 0 and 99 inclusively, it means that the first 50 numbers it can give you are: 0, 1, 2, 3... all the way to 49. And the next 50 are: 50, 51, 52, 53... all the way to 99. You can imagine a 100-sided dice labeled from 0 to 99 if you wish.
If you want your turtles to "become fat" 50% of the time, you can do:
ask turtles [ if random 100 < 50 [become-fat] ]
Notice that I used the <
(strictly less) sign instead of the <=
(less or equal) sign. This is because I only want the turtles to become fat if the "dice" lands on one of the first 50 faces, from 0 to 49.
(If you used random 100 <= 50
, like in the code you posted above, they would actually have a 51% probability of becoming fat and a 49% probability to not become fat. You should now also be able to figure out why something like if random 100 = 50
doesn't make sense: it would only be true if the "dice" lands exactly on 50, which happens only 1% of the time.)
If you wanted your turtles to become fat only 20% of the time, you'd want to use the first 20 faces of the dice, from 0 to 19:
ask turtles [ if random 100 < 20 [become-fat] ]
It is often enough to use random 100
when dealing with probabilities in NetLogo.
Using random-float
Sometimes, however, you need a little more precision. And mathematically oriented work often expresses probabilities as numbers between 0.0 (for 0%) and 1.0 (for 100%). In those cases, random-float 1
comes handy. Again, as stated in the documentation, random-float
will give you a number between 0 (inclusively) and the number you pass to it (exclusively). Thus, random-float 1
gives you a number between 0.0 and 1.0 (but never exactly 1.0).
This expression:
random-float 1 < 0.025
will be true 2.5% of the time.
The dice metaphor doesn't work for random-float
, but you can imagine a roulette wheel (or a wheel of fortune). Asking if random-float 1 < 0.025
is like painting a "pie slice" that's 2.5% of the circumference of the wheel, spinning the wheel, and checking if the ball (or the arrow, or whatever) falls in that slice.
Now does it matter if you use <=
instead of <
with random-float
? Not a whole lot. It would only make a difference if the wheel falls exactly on the line that separates your pie slice from the rest of the wheel, and the probability of that happening is very very small.
来源:https://stackoverflow.com/questions/24898582/interpretations-of-probabilities-and-percentages