4 ms·
Both approaches have their advantages, IMO. Inclusive ranges are usually easier to understand, but e.g. when picking a (uniformly distributed) random element fr
by tgv 6y ago
Both approaches have their advantages, IMO. Inclusive ranges are usually easier to understand, but e.g. when picking a (uniformly distributed) random element from an array, nothing beats a[floor(rnd() * len(a)].
- dunefox 6y ago> nothing beats a[floor(rnd() * len(a)] What's wrong with rand(a)?
- socialdemocrat 6y agoHow is that very readable? In Julia, this will pick a random value from an array: rand([3, 4, 5, 1]) While this picks a value from a range: rand(1:10) Much more straightforward to read IMHO. Where I prefer 0-based index is when dealing with coordinate systems and memory locations.
- deleted 6y ago[deleted]
- vcxy 6y agoAlready mentioned, in julia just do rand(a). But if you didn't have that, couldn't you just switch out floor for ceil and have the exact equivalent for 1 based indexing?
- Sebb767 6y agoNo - rnd() can not return one, but can return zero, at which point you'd access index 0 even with ceil().
- vcxy 6y agoAh, thanks! I think I'll stick with rand(indexable_collection) in Julia since anything else just doesn't feel Julian, but I'll remember that for other 1-based languages if I come across it.