WeixiangYan/CodeTransOcean huggingface dataset:
Task: Tic-tac-toe







Python: 

import random

board = list('123456789')
wins = ((0,1,2), (3,4,5), (6,7,8),
        (0,3,6), (1,4,7), (2,5,8),
        (0,4,8), (2,4,6))

def printboard():
    print('\n'.join(' '.join(board[x:x+3]) for x in(0,3,6)))

def score():
    for w in wins:
        b = board[w[0]]
        if b in 'XO' and all (board[i] == b for i in w):
            return b, [i+1 for i in w]
    return None, None

def finished():
    return all (b in 'XO' for b in board)

def space():
    return [ b for b in board if b not in 'XO']

def my_turn(xo):
    options = space()
    choice = random.choice(options)
    board[int(choice)-1] = xo
    return choice

def your_turn(xo):
    options = space()
    while True:
        choice = input(" Put your %s in any of these positions: %s "
                       % (xo, ''.join(options))).strip()
        if choice in options:
            break
        print( "Whoops I don't understand the input" )
    board[int(choice)-1] = xo
    return choice

def me(xo='X'):
    printboard()
    print('I go at', my_turn(xo))
    return score()
    assert not s[0], "\n%s wins across %s" % s

def you(xo='O'):
    printboard()
    
    print('You went at', your_turn(xo))
    return score()
    assert not s[0], "\n%s wins across %s" % s


print(__doc__)
while not finished():
    s = me('X')
    if s[0]:
        printboard()
        print("\n%s wins across %s" % s)
        break
    if not finished():
        s = you('O')
        if s[0]:
            printboard()
            print("\n%s wins across %s" % s)
            break
else:
    print('\nA draw')
Task: Tic-tac-toe






PHP: <?php
const BOARD_NUM = 9;
const ROW_NUM = 3;
$EMPTY_BOARD_STR = str_repeat('.', BOARD_NUM);

function isGameOver($board, $pin) {
	$pat = 
		'/X{3}|' . //Horz
		'X..X..X..|' . //Vert Left
		'.X..X..X.|' . //Vert Middle
		'..X..X..X|' . //Vert Right
		'..X.X.X..|' . //Diag TL->BR
		'X...X...X|' . //Diag TR->BL
		'[^\.]{9}/i'; //Cat's game
	if ($pin == 'O') $pat = str_replace('X', 'O', $pat);
	return preg_match($pat, $board);
}

$boardStr = isset($_GET['b'])? $_GET['b'] : $EMPTY_BOARD_STR;
$turn = substr_count($boardStr, '.')%2==0? 'O' : 'X';
$oppTurn = $turn == 'X'? 'O' : 'X';
$gameOver = isGameOver($boardStr, $oppTurn);

echo '<style>';
echo 'td {width: 200px; height: 200px; text-align: center; }';
echo '.pin {font-size:72pt; text-decoration:none; color: black}';
echo '.pin.X {color:red}';
echo '.pin.O {color:blue}';
echo '</style>';
echo '<table border="1">';
$p = 0;
for ($r = 0; $r < ROW_NUM; $r++) {
	echo '<tr>';
	for ($c = 0; $c < ROW_NUM; $c++) {
		$pin = $boardStr[$p];
		
		echo '<td>';		
		if ($gameOver || $pin != '.') echo '<span class="pin ', $pin, '">', $pin, '</span>'; //Occupied
		else { //Available
			$boardDelta = $boardStr;
			$boardDelta[$p] = $turn;		
			echo '<a class="pin ', $pin, '" href="?b=', $boardDelta, '">';
			echo $boardStr[$p];
			echo '</a>';
		}
		
		echo '</td>';	
		$p++;
	}
	echo '</tr>';
	echo '<input type="hidden" name="b" value="', $boardStr, '"/>';
}
echo '</table>';
echo '<a href="?b=', $EMPTY_BOARD_STR, '">Reset</a>';
if ($gameOver) echo '<h1>Game Over!</h1>';
Task: Pick random element



C#: using System;
using System.Collections.Generic;

class RandomElementPicker {
  static void Main() {
    var list = new List<int>(new[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
    var rng = new Random();
    var randomElement = list[rng.Next(list.Count)];
    Console.WriteLine("I picked element {0}", randomElement);
  }
}









Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element

C: #include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(){
   char array[] = { 'a', 'b', 'c','d','e','f','g','h','i','j' };
   int i;
   time_t t;
   srand((unsigned)time(&t));
   
   for(i=0;i<30;i++){
		printf("%c\n", array[rand()%10]);
   }
   
   return 0;
}











Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element


C++: #include <iostream>
#include <random>
#include <vector>

int main( ) {
   std::vector<int> numbers { 11 , 88 , -5 , 13 , 4 , 121 , 77 , 2 } ;
   std::random_device seed ;
   
   std::mt19937 engine( seed( ) ) ;
   
   std::uniform_int_distribution<int> choose( 0 , numbers.size( ) - 1 ) ;
   std::cout << "random element picked : " << numbers[ choose( engine ) ] 
      << " !\n" ;
   return 0 ;
}










Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

var list = []string{"bleen", "fuligin", "garrow", "grue", "hooloovoo"}

func main() {
    rand.Seed(time.Now().UnixNano())
    fmt.Println(list[rand.Intn(len(list))])
}








Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element





Java: import java.util.Random;
...
int[] array = {1,2,3};
return array[new Random().nextInt(array.length)]; 







Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element







Python: >>> import random
>>> random.choice(['foo', 'bar', 'baz'])
'baz'





Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element








VB: Option Explicit

Sub Main_Pick_Random_Element()
    Debug.Print Pick_Random_Element(Array(1, 2, 3, 4, 5, #11/24/2017#, "azerty"))
End Sub

Function Pick_Random_Element(myArray)
    Randomize Timer
    Pick_Random_Element = myArray(Int((Rnd * (UBound(myArray) - LBound(myArray) + 1) + LBound(myArray))))
End Function




Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element

C: #include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(){
   char array[] = { 'a', 'b', 'c','d','e','f','g','h','i','j' };
   int i;
   time_t t;
   srand((unsigned)time(&t));
   
   for(i=0;i<30;i++){
		printf("%c\n", array[rand()%10]);
   }
   
   return 0;
}
Task: Pick random element



C#: using System;
using System.Collections.Generic;

class RandomElementPicker {
  static void Main() {
    var list = new List<int>(new[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
    var rng = new Random();
    var randomElement = list[rng.Next(list.Count)];
    Console.WriteLine("I picked element {0}", randomElement);
  }
}
Task: Pick random element


C++: #include <iostream>
#include <random>
#include <vector>

int main( ) {
   std::vector<int> numbers { 11 , 88 , -5 , 13 , 4 , 121 , 77 , 2 } ;
   std::random_device seed ;
   
   std::mt19937 engine( seed( ) ) ;
   
   std::uniform_int_distribution<int> choose( 0 , numbers.size( ) - 1 ) ;
   std::cout << "random element picked : " << numbers[ choose( engine ) ] 
      << " !\n" ;
   return 0 ;
}
Task: Pick random element





Java: import java.util.Random;
...
int[] array = {1,2,3};
return array[new Random().nextInt(array.length)];
Task: Pick random element







Python: >>> import random
>>> random.choice(['foo', 'bar', 'baz'])
'baz'
Task: Pick random element








VB: Option Explicit

Sub Main_Pick_Random_Element()
    Debug.Print Pick_Random_Element(Array(1, 2, 3, 4, 5, #11/24/2017#, "azerty"))
End Sub

Function Pick_Random_Element(myArray)
    Randomize Timer
    Pick_Random_Element = myArray(Int((Rnd * (UBound(myArray) - LBound(myArray) + 1) + LBound(myArray))))
End Function
Task: Pick random element




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

var list = []string{"bleen", "fuligin", "garrow", "grue", "hooloovoo"}

func main() {
    rand.Seed(time.Now().UnixNano())
    fmt.Println(list[rand.Intn(len(list))])
}
Task: Pick random element



C#: using System;
using System.Collections.Generic;

class RandomElementPicker {
  static void Main() {
    var list = new List<int>(new[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
    var rng = new Random();
    var randomElement = list[rng.Next(list.Count)];
    Console.WriteLine("I picked element {0}", randomElement);
  }
}








Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element


C++: #include <iostream>
#include <random>
#include <vector>

int main( ) {
   std::vector<int> numbers { 11 , 88 , -5 , 13 , 4 , 121 , 77 , 2 } ;
   std::random_device seed ;
   
   std::mt19937 engine( seed( ) ) ;
   
   std::uniform_int_distribution<int> choose( 0 , numbers.size( ) - 1 ) ;
   std::cout << "random element picked : " << numbers[ choose( engine ) ] 
      << " !\n" ;
   return 0 ;
}









Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element

C: #include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(){
   char array[] = { 'a', 'b', 'c','d','e','f','g','h','i','j' };
   int i;
   time_t t;
   srand((unsigned)time(&t));
   
   for(i=0;i<30;i++){
		printf("%c\n", array[rand()%10]);
   }
   
   return 0;
}










Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

var list = []string{"bleen", "fuligin", "garrow", "grue", "hooloovoo"}

func main() {
    rand.Seed(time.Now().UnixNano())
    fmt.Println(list[rand.Intn(len(list))])
}







Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element





Java: import java.util.Random;
...
int[] array = {1,2,3};
return array[new Random().nextInt(array.length)]; 






Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element







Python: >>> import random
>>> random.choice(['foo', 'bar', 'baz'])
'baz'




Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element








VB: Option Explicit

Sub Main_Pick_Random_Element()
    Debug.Print Pick_Random_Element(Array(1, 2, 3, 4, 5, #11/24/2017#, "azerty"))
End Sub

Function Pick_Random_Element(myArray)
    Randomize Timer
    Pick_Random_Element = myArray(Int((Rnd * (UBound(myArray) - LBound(myArray) + 1) + LBound(myArray))))
End Function



Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element






PHP: $arr = array('foo', 'bar', 'baz');
$x = $arr[array_rand($arr)];





Fortran: program pick_random
  implicit none

  integer :: i
  integer :: a(10) = (/ (i, i = 1, 10) /)
  real :: r

  call random_seed
  call random_number(r)
  write(*,*) a(int(r*size(a)) + 1)
end program
Task: Pick random element

C: #include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(){
   char array[] = { 'a', 'b', 'c','d','e','f','g','h','i','j' };
   int i;
   time_t t;
   srand((unsigned)time(&t));
   
   for(i=0;i<30;i++){
		printf("%c\n", array[rand()%10]);
   }
   
   return 0;
}









COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.
Task: Pick random element



C#: using System;
using System.Collections.Generic;

class RandomElementPicker {
  static void Main() {
    var list = new List<int>(new[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
    var rng = new Random();
    var randomElement = list[rng.Next(list.Count)];
    Console.WriteLine("I picked element {0}", randomElement);
  }
}







COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.
Task: Pick random element


C++: #include <iostream>
#include <random>
#include <vector>

int main( ) {
   std::vector<int> numbers { 11 , 88 , -5 , 13 , 4 , 121 , 77 , 2 } ;
   std::random_device seed ;
   
   std::mt19937 engine( seed( ) ) ;
   
   std::uniform_int_distribution<int> choose( 0 , numbers.size( ) - 1 ) ;
   std::cout << "random element picked : " << numbers[ choose( engine ) ] 
      << " !\n" ;
   return 0 ;
}








COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.
Task: Pick random element





Java: import java.util.Random;
...
int[] array = {1,2,3};
return array[new Random().nextInt(array.length)]; 





COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.
Task: Pick random element







Python: >>> import random
>>> random.choice(['foo', 'bar', 'baz'])
'baz'



COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.
Task: Pick random element








VB: Option Explicit

Sub Main_Pick_Random_Element()
    Debug.Print Pick_Random_Element(Array(1, 2, 3, 4, 5, #11/24/2017#, "azerty"))
End Sub

Function Pick_Random_Element(myArray)
    Randomize Timer
    Pick_Random_Element = myArray(Int((Rnd * (UBound(myArray) - LBound(myArray) + 1) + LBound(myArray))))
End Function


COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.
Task: Pick random element




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

var list = []string{"bleen", "fuligin", "garrow", "grue", "hooloovoo"}

func main() {
    rand.Seed(time.Now().UnixNano())
    fmt.Println(list[rand.Intn(len(list))])
}






COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.
Task: Pick random element






PHP: $arr = array('foo', 'bar', 'baz');
$x = $arr[array_rand($arr)];
Task: Pick random element






PHP: $arr = array('foo', 'bar', 'baz');
$x = $arr[array_rand($arr)];






Ada: with Ada.Text_IO, Ada.Numerics.Float_Random;

procedure Pick_Random_Element is

   package Rnd renames Ada.Numerics.Float_Random;
   Gen: Rnd.Generator; 

   type Char_Arr is array (Natural range <>) of Character;

   function Pick_Random(A: Char_Arr) return Character is
      
   begin
      return A(A'First + Natural(Rnd.Random(Gen) * Float(A'Last)));
   end Pick_Random;

   Vowels    : Char_Arr := ('a', 'e', 'i', 'o', 'u');
   Consonants: Char_Arr := ('t', 'n', 's', 'h', 'r', 'd', 'l');
   Specials  : Char_Arr := (',', '.', '?', '!');

begin
   Rnd.Reset(Gen);
   for J in 1 .. 3 loop
      for I in 1 .. 10 loop
         Ada.Text_IO.Put(Pick_Random(Consonants));
         Ada.Text_IO.Put(Pick_Random(Vowels));
      end loop;
      Ada.Text_IO.Put(Pick_Random(Specials) & " ");
   end loop;
   Ada.Text_IO.New_Line;
end Pick_Random_Element;
Task: Pick random element






PHP: $arr = array('foo', 'bar', 'baz');
$x = $arr[array_rand($arr)];




COBOL:        >>SOURCE FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. random-element.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  nums-area                           VALUE "123456789".
    03  nums                            PIC 9 OCCURS 9 TIMES.
    
01  random-idx                          PIC 9 COMP.
    
PROCEDURE DIVISION.
    COMPUTE random-idx = FUNCTION RANDOM(FUNCTION CURRENT-DATE (9:7)) * 9 + 1
    DISPLAY nums (random-idx)
    .
END PROGRAM random-element.