Shreesha Vaidhya
Shreesha Vaidhya

Reputation: 63

NetLogo: Neet to set the variable's value to a random number within a range

Dear NetLogo Community,

I am aiming to set a variable's value to a number between -1 and 1. I have tried the following code but in vain.

to xyz
[
set probability-of-wom compute-wom [-1 1]
if probability-wom > 0 [...]
]
end

to-report compute-wom -1 1
  report -1 + random-float (1 - -1)
end

probablity-wom is a global variable in this case. Appreciate your support in advance. Thank you.

Regards, Shreesha

Upvotes: 1

Views: 237

Answers (1)

Charles
Charles

Reputation: 4168

Shreesha

Let's say you want to generate a random number between lower and upper. Then your compute-wom would be

to-report compute-wom [lower upper]
  report upper - random-float (upper - lower)
end

In your case, you would

set probability-of-wom compute-wom -1 1

But a couple of comments. First, what you are generating here is a random number between two limits (as your title suggests), so calling it a probability could be misleading to anyone reading your code. Probabilities will normally be in the range from zero to one. If you really are just looking to do something with a 50% probability, you can simply say

if random-float 1 >= 0.5 [...]

Second, reporters should generally take variable arguments if they are to have arguments at all. Note that since you hard code -1 and 1 in the body of your to-report compute-wom reporter, passing them as arguments is redundant, and possibly misleading to anyone reading your code.

Upvotes: 1

Related Questions