Update_random
Update Random replaces a random value in a collection.
Overview
update_random replaces one randomly selected value in a collection.
items | [1, 2, 3] :update_random!(items, 99)
items | [1, 2, 3] :update_random!(items, 99)
Example result:
[1, 99, 3]
[1, 99, 3]
The replaced value may be different each time.
Mutation
Like most collection utilities, update_random follows Goblin's bang (!) rules.
:update_randomreturns a modified copy:update_random!mutates the original collection
Similar Operations In Other Languages
| JavaScript | Python | Java | C# | Ruby | PHP |
|---|---|---|---|---|---|
| assignment | assignment | set() |
assignment | []= |
assignment |
splice() |
slice assignment | replaceAll() |
Replace() |
replace |
array_replace() |
fill() |
list comprehension | update loop | Select() |
map! |
array_map() |