5

I am new programmer in netlogo. I waned to know how can I pick a random string from a list/array of strings in netLogo? If anyone can please guide me, I will be very much thankful. Thanks

PS : I have done efforts on my part, I only know how to pick a random number from a number range like this

random-float (PRICE * 0.20) ;For float value (price is an integer defined by me)
random round (DifferenceAmt * 0.10) ; For random rounded amount

1 Answer 1

7

Simply use one-of, e.g.:

print one-of [ "a" "b" "c" ]

will print either "a", "b" or "c", at random.

Sign up to request clarification or add additional context in comments.

3 Comments

I noticed that you were asking for both lists and arrays. Note that one-of doesn't work with arrays from the array extension, but those can be converted to a list with array:to-list, so you can do: one-of array:to-list my-array. If speed is crucial, you could also do array:item my-array one-of n-values array:length my-array [ ? ]. But unless you have a very specific need for an array, you probably should not be using one.
Thank you so much for your help sir. This will do just fine for now :)
My pleasure. And by the way, the "fast" array version should have been: array:item my-array random array:length. (I don't think I was quite awake when I suggested using n-values for that...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.