- Ada
- C
- Clojure
- C++
- C++
- C#
- C#
- D
- Dart
- Elixir
- Erlang
- Erlang
- Fortran
- Go
- Go
- Go
- Groovy
- Haskell
- Haskell
- JS
- JS
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Pascal
- Perl
- Ruby
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
- VB
subtype Element_Type is Integer;
type List_Type is array (Positive range <>) of Element_Type;
function Pick_Random (X : List_Type) return Element_Type is
subtype Index_Range is Positive range X'Range;
package Random_Index is new
Ada.Numerics.Discrete_Random (Index_Range);
Generator : Random_Index.Generator;
begin
Random_Index.Reset (Generator);
return X (Random_Index.Random (Generator));
end Pick_Random;
x[Math.floor(Math.random() * x.length)]
Note that Math.random is not cryptographically secure.
x.get((int)(Math.random()*x.size()))
Consider reusing the Random object, don't create it each time you pick an element.
if (!x.isEmpty()) {
Random r = new Random();
T t = x.get(r.nextInt(x.size()));
}
my @x = ('a', 'list', 'of', 'random', 'items');
print $x[rand @x];
In scalar context (such as the call to rand), an array evaluates to the length.