WeixiangYan/CodeTransOcean huggingface dataset:
Task: Happy numbers



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

namespace HappyNums
{
    class Program
    {
        public static bool ishappy(int n)
        {
            List<int> cache = new List<int>();
            int sum = 0;
            while (n != 1)
            {
                if (cache.Contains(n))
                {
                    return false;
                }
                cache.Add(n);
                while (n != 0)
                {
                    int digit = n % 10;
                    sum += digit * digit;
                    n /= 10;
                }
                n = sum;
                sum = 0;
            }
           return true;            
        }

        static void Main(string[] args)
        {
            int num = 1;
            List<int> happynums = new List<int>();

            while (happynums.Count < 8)
            {
                if (ishappy(num))
                {
                    happynums.Add(num);
                }
                num++;
            }
            Console.WriteLine("First 8 happy numbers : " + string.Join(",", happynums));
        }
    }
}







COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. HAPPY.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE        PIC 9(4).
          03 SQSUM-IN         PIC 9(4).
          03 FILLER           REDEFINES SQSUM-IN. 
             05 DIGITS        PIC 9 OCCURS 4 TIMES.
          03 SQUARE           PIC 9(4).
          03 SUM-OF-SQUARES   PIC 9(4).
          03 N                PIC 9.
          03 TORTOISE         PIC 9(4).
          03 HARE             PIC 9(4).
             88 HAPPY         VALUE 1.
          03 SEEN             PIC 9 VALUE ZERO.
          03 OUT-FMT          PIC ZZZ9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
           UNTIL SEEN IS EQUAL TO 8.
           STOP RUN.

       DISPLAY-IF-HAPPY.
           PERFORM CHECK-HAPPY.
           IF HAPPY, 
               MOVE CANDIDATE TO OUT-FMT, 
               DISPLAY OUT-FMT,
               ADD 1 TO SEEN.
 
       CHECK-HAPPY.
           MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
           PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
    
       CHECK-HAPPY-STEP.
           MOVE TORTOISE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO TORTOISE.
           MOVE HARE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
 
       CALC-SUM-OF-SQUARES.
           MOVE ZERO TO SUM-OF-SQUARES.
           PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
           UNTIL N IS GREATER THAN 4.
 
       ADD-DIGIT-SQUARE.
           MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
           ADD SQUARE TO SUM-OF-SQUARES.
Task: Happy numbers


C++: #include <map>
#include <set>

bool happy(int number) {
  static std::map<int, bool> cache;

  std::set<int> cycle;
  while (number != 1 && !cycle.count(number)) {
    if (cache.count(number)) {
      number = cache[number] ? 1 : 0;
      break;
    }
    cycle.insert(number);
    int newnumber = 0;
    while (number > 0) {
      int digit = number % 10;
      newnumber += digit * digit;
      number /= 10;
    }
    number = newnumber;
  }
  bool happiness = number == 1;
  for (std::set<int>::const_iterator it = cycle.begin();
       it != cycle.end(); it++)
    cache[*it] = happiness;
  return happiness;
}

#include <iostream>

int main() {
  for (int i = 1; i < 50; i++)
    if (happy(i))
      std::cout << i << std::endl;
  return 0;
}








COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. HAPPY.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE        PIC 9(4).
          03 SQSUM-IN         PIC 9(4).
          03 FILLER           REDEFINES SQSUM-IN. 
             05 DIGITS        PIC 9 OCCURS 4 TIMES.
          03 SQUARE           PIC 9(4).
          03 SUM-OF-SQUARES   PIC 9(4).
          03 N                PIC 9.
          03 TORTOISE         PIC 9(4).
          03 HARE             PIC 9(4).
             88 HAPPY         VALUE 1.
          03 SEEN             PIC 9 VALUE ZERO.
          03 OUT-FMT          PIC ZZZ9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
           UNTIL SEEN IS EQUAL TO 8.
           STOP RUN.

       DISPLAY-IF-HAPPY.
           PERFORM CHECK-HAPPY.
           IF HAPPY, 
               MOVE CANDIDATE TO OUT-FMT, 
               DISPLAY OUT-FMT,
               ADD 1 TO SEEN.
 
       CHECK-HAPPY.
           MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
           PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
    
       CHECK-HAPPY-STEP.
           MOVE TORTOISE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO TORTOISE.
           MOVE HARE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
 
       CALC-SUM-OF-SQUARES.
           MOVE ZERO TO SUM-OF-SQUARES.
           PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
           UNTIL N IS GREATER THAN 4.
 
       ADD-DIGIT-SQUARE.
           MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
           ADD SQUARE TO SUM-OF-SQUARES.
Task: Happy numbers





Java: import java.util.HashSet;
public class Happy{
   public static boolean happy(long number){
       long m = 0;
       int digit = 0;
       HashSet<Long> cycle = new HashSet<Long>();
       while(number != 1 && cycle.add(number)){
           m = 0;
           while(number > 0){
               digit = (int)(number % 10);
               m += digit*digit;
               number /= 10;
           }
           number = m;
       }
       return number == 1;
   }

   public static void main(String[] args){
       for(long num = 1,count = 0;count<8;num++){
           if(happy(num)){
               System.out.println(num);
               count++;
           }
       }
   }
}





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. HAPPY.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE        PIC 9(4).
          03 SQSUM-IN         PIC 9(4).
          03 FILLER           REDEFINES SQSUM-IN. 
             05 DIGITS        PIC 9 OCCURS 4 TIMES.
          03 SQUARE           PIC 9(4).
          03 SUM-OF-SQUARES   PIC 9(4).
          03 N                PIC 9.
          03 TORTOISE         PIC 9(4).
          03 HARE             PIC 9(4).
             88 HAPPY         VALUE 1.
          03 SEEN             PIC 9 VALUE ZERO.
          03 OUT-FMT          PIC ZZZ9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
           UNTIL SEEN IS EQUAL TO 8.
           STOP RUN.

       DISPLAY-IF-HAPPY.
           PERFORM CHECK-HAPPY.
           IF HAPPY, 
               MOVE CANDIDATE TO OUT-FMT, 
               DISPLAY OUT-FMT,
               ADD 1 TO SEEN.
 
       CHECK-HAPPY.
           MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
           PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
    
       CHECK-HAPPY-STEP.
           MOVE TORTOISE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO TORTOISE.
           MOVE HARE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
 
       CALC-SUM-OF-SQUARES.
           MOVE ZERO TO SUM-OF-SQUARES.
           PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
           UNTIL N IS GREATER THAN 4.
 
       ADD-DIGIT-SQUARE.
           MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
           ADD SQUARE TO SUM-OF-SQUARES.
Task: Happy numbers







Python: >>> def happy(n):
    past = set()			
    while n != 1:
        n = sum(int(i)**2 for i in str(n))
        if n in past:
            return False
        past.add(n)
    return True

>>> [x for x in xrange(500) if happy(x)][:8]
[1, 7, 10, 13, 19, 23, 28, 31]



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. HAPPY.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE        PIC 9(4).
          03 SQSUM-IN         PIC 9(4).
          03 FILLER           REDEFINES SQSUM-IN. 
             05 DIGITS        PIC 9 OCCURS 4 TIMES.
          03 SQUARE           PIC 9(4).
          03 SUM-OF-SQUARES   PIC 9(4).
          03 N                PIC 9.
          03 TORTOISE         PIC 9(4).
          03 HARE             PIC 9(4).
             88 HAPPY         VALUE 1.
          03 SEEN             PIC 9 VALUE ZERO.
          03 OUT-FMT          PIC ZZZ9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
           UNTIL SEEN IS EQUAL TO 8.
           STOP RUN.

       DISPLAY-IF-HAPPY.
           PERFORM CHECK-HAPPY.
           IF HAPPY, 
               MOVE CANDIDATE TO OUT-FMT, 
               DISPLAY OUT-FMT,
               ADD 1 TO SEEN.
 
       CHECK-HAPPY.
           MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
           PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
    
       CHECK-HAPPY-STEP.
           MOVE TORTOISE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO TORTOISE.
           MOVE HARE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
 
       CALC-SUM-OF-SQUARES.
           MOVE ZERO TO SUM-OF-SQUARES.
           PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
           UNTIL N IS GREATER THAN 4.
 
       ADD-DIGIT-SQUARE.
           MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
           ADD SQUARE TO SUM-OF-SQUARES.
Task: Happy numbers








VB: Option Explicit

Sub Test_Happy()
Dim i&, Cpt&

    For i = 1 To 100
        If Is_Happy_Number(i) Then
            Debug.Print "Is Happy : " & i
            Cpt = Cpt + 1
            If Cpt = 8 Then Exit For
        End If
    Next
End Sub

Public Function Is_Happy_Number(ByVal N As Long) As Boolean
Dim i&, Number$, Cpt&
    Is_Happy_Number = False 
    Do
        Cpt = Cpt + 1       
        Number = CStr(N)    
        N = 0
        For i = 1 To Len(Number)
            N = N + CInt(Mid(Number, i, 1)) ^ 2
        Next i
        
        If Cpt = 50 Then Exit Function
    Loop Until N = 1
    Is_Happy_Number = True
End Function


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. HAPPY.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE        PIC 9(4).
          03 SQSUM-IN         PIC 9(4).
          03 FILLER           REDEFINES SQSUM-IN. 
             05 DIGITS        PIC 9 OCCURS 4 TIMES.
          03 SQUARE           PIC 9(4).
          03 SUM-OF-SQUARES   PIC 9(4).
          03 N                PIC 9.
          03 TORTOISE         PIC 9(4).
          03 HARE             PIC 9(4).
             88 HAPPY         VALUE 1.
          03 SEEN             PIC 9 VALUE ZERO.
          03 OUT-FMT          PIC ZZZ9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
           UNTIL SEEN IS EQUAL TO 8.
           STOP RUN.

       DISPLAY-IF-HAPPY.
           PERFORM CHECK-HAPPY.
           IF HAPPY, 
               MOVE CANDIDATE TO OUT-FMT, 
               DISPLAY OUT-FMT,
               ADD 1 TO SEEN.
 
       CHECK-HAPPY.
           MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
           PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
    
       CHECK-HAPPY-STEP.
           MOVE TORTOISE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO TORTOISE.
           MOVE HARE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
 
       CALC-SUM-OF-SQUARES.
           MOVE ZERO TO SUM-OF-SQUARES.
           PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
           UNTIL N IS GREATER THAN 4.
 
       ADD-DIGIT-SQUARE.
           MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
           ADD SQUARE TO SUM-OF-SQUARES.
Task: Happy numbers




Go: package main

import "fmt"

func happy(n int) bool {
	m := make(map[int]bool)
	for n > 1 {
		m[n] = true
		var x int
		for x, n = n, 0; x > 0; x /= 10 {
			d := x % 10
			n += d * d
		}
		if m[n] {
			return false
		}
	}
	return true
}

func main() {
	for found, n := 0, 1; found < 8; n++ {
		if happy(n) {
			fmt.Print(n, " ")
			found++
		}
	}
	fmt.Println()
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. HAPPY.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE        PIC 9(4).
          03 SQSUM-IN         PIC 9(4).
          03 FILLER           REDEFINES SQSUM-IN. 
             05 DIGITS        PIC 9 OCCURS 4 TIMES.
          03 SQUARE           PIC 9(4).
          03 SUM-OF-SQUARES   PIC 9(4).
          03 N                PIC 9.
          03 TORTOISE         PIC 9(4).
          03 HARE             PIC 9(4).
             88 HAPPY         VALUE 1.
          03 SEEN             PIC 9 VALUE ZERO.
          03 OUT-FMT          PIC ZZZ9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
           UNTIL SEEN IS EQUAL TO 8.
           STOP RUN.

       DISPLAY-IF-HAPPY.
           PERFORM CHECK-HAPPY.
           IF HAPPY, 
               MOVE CANDIDATE TO OUT-FMT, 
               DISPLAY OUT-FMT,
               ADD 1 TO SEEN.
 
       CHECK-HAPPY.
           MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
           PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
    
       CHECK-HAPPY-STEP.
           MOVE TORTOISE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO TORTOISE.
           MOVE HARE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
 
       CALC-SUM-OF-SQUARES.
           MOVE ZERO TO SUM-OF-SQUARES.
           PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
           UNTIL N IS GREATER THAN 4.
 
       ADD-DIGIT-SQUARE.
           MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
           ADD SQUARE TO SUM-OF-SQUARES.
Task: Happy numbers






PHP: function isHappy($n) {
    while (1) {
        $total = 0;
        while ($n > 0) {
            $total += pow(($n % 10), 2);
            $n /= 10;
        }
        if ($total == 1)
            return true;
        if (array_key_exists($total, $past))
            return false;
        $n = $total;
        $past[$total] = 0;
    }
}

$i = $cnt = 0;
while ($cnt < 8) {
    if (isHappy($i)) {
        echo "$i ";
        $cnt++;
    }
    $i++;
}
Task: Happy numbers






PHP: function isHappy($n) {
    while (1) {
        $total = 0;
        while ($n > 0) {
            $total += pow(($n % 10), 2);
            $n /= 10;
        }
        if ($total == 1)
            return true;
        if (array_key_exists($total, $past))
            return false;
        $n = $total;
        $past[$total] = 0;
    }
}

$i = $cnt = 0;
while ($cnt < 8) {
    if (isHappy($i)) {
        echo "$i ";
        $cnt++;
    }
    $i++;
}






Ada: with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Containers.Ordered_Sets;

procedure Test_Happy_Digits is
   function Is_Happy (N : Positive) return Boolean is
      package Sets_Of_Positive is new Ada.Containers.Ordered_Sets (Positive);
      use Sets_Of_Positive;
      function Next (N : Positive) return Natural is
         Sum   : Natural := 0;
         Accum : Natural := N;
      begin
         while Accum > 0 loop
            Sum   := Sum + (Accum mod 10) ** 2;
            Accum := Accum / 10;
         end loop;
         return Sum;
      end Next;
      Current : Positive := N;
      Visited : Set;
   begin
      loop
         if Current = 1 then
            return True;
         elsif Visited.Contains (Current) then
            return False;
         else
            Visited.Insert (Current);
            Current := Next (Current);
         end if;
      end loop;
   end Is_Happy;
   Found : Natural := 0;
begin
   for N in Positive'Range loop
      if Is_Happy (N) then
         Put (Integer'Image (N));
         Found := Found + 1;
         exit when Found = 8;
      end if;
   end loop;
end Test_Happy_Digits;
Task: Happy numbers






PHP: function isHappy($n) {
    while (1) {
        $total = 0;
        while ($n > 0) {
            $total += pow(($n % 10), 2);
            $n /= 10;
        }
        if ($total == 1)
            return true;
        if (array_key_exists($total, $past))
            return false;
        $n = $total;
        $past[$total] = 0;
    }
}

$i = $cnt = 0;
while ($cnt < 8) {
    if (isHappy($i)) {
        echo "$i ";
        $cnt++;
    }
    $i++;
}




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. HAPPY.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 CANDIDATE        PIC 9(4).
          03 SQSUM-IN         PIC 9(4).
          03 FILLER           REDEFINES SQSUM-IN. 
             05 DIGITS        PIC 9 OCCURS 4 TIMES.
          03 SQUARE           PIC 9(4).
          03 SUM-OF-SQUARES   PIC 9(4).
          03 N                PIC 9.
          03 TORTOISE         PIC 9(4).
          03 HARE             PIC 9(4).
             88 HAPPY         VALUE 1.
          03 SEEN             PIC 9 VALUE ZERO.
          03 OUT-FMT          PIC ZZZ9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM DISPLAY-IF-HAPPY VARYING CANDIDATE FROM 1 BY 1
           UNTIL SEEN IS EQUAL TO 8.
           STOP RUN.

       DISPLAY-IF-HAPPY.
           PERFORM CHECK-HAPPY.
           IF HAPPY, 
               MOVE CANDIDATE TO OUT-FMT, 
               DISPLAY OUT-FMT,
               ADD 1 TO SEEN.
 
       CHECK-HAPPY.
           MOVE CANDIDATE TO TORTOISE, SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
           PERFORM CHECK-HAPPY-STEP UNTIL TORTOISE IS EQUAL TO HARE.
    
       CHECK-HAPPY-STEP.
           MOVE TORTOISE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO TORTOISE.
           MOVE HARE TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO SQSUM-IN.
           PERFORM CALC-SUM-OF-SQUARES.
           MOVE SUM-OF-SQUARES TO HARE.
 
       CALC-SUM-OF-SQUARES.
           MOVE ZERO TO SUM-OF-SQUARES.
           PERFORM ADD-DIGIT-SQUARE VARYING N FROM 1 BY 1
           UNTIL N IS GREATER THAN 4.
 
       ADD-DIGIT-SQUARE.
           MULTIPLY DIGITS(N) BY DIGITS(N) GIVING SQUARE.
           ADD SQUARE TO SUM-OF-SQUARES.
Task: Random numbers



C#: private static double randomNormal()
{
	return Math.Cos(2 * Math.PI * tRand.NextDouble()) * Math.Sqrt(-2 * Math.Log(tRand.NextDouble()));
}









Ada: with Ada.Numerics;                       use Ada.Numerics;
with Ada.Numerics.Float_Random;          use Ada.Numerics.Float_Random;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;

procedure Normal_Random is
   function Normal_Distribution
            (  Seed  : Generator;
               Mu    : Float := 1.0;
               Sigma : Float := 0.5
            )  return Float is 
   begin
      return
         Mu + (Sigma * Sqrt (-2.0 * Log (Random (Seed), 10.0)) * Cos (2.0 * Pi * Random (Seed)));
   end Normal_Distribution;
      
   Seed         : Generator;
   Distribution : array (1..1_000) of Float; 
begin
   Reset (Seed);
   for I in Distribution'Range loop
      Distribution (I) := Normal_Distribution (Seed);
   end loop;
end Normal_Random;
Task: Random numbers

C: #include <stdlib.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

double drand()   
{
  return (rand()+1.0)/(RAND_MAX+1.0);
}
double random_normal()  
{
  return sqrt(-2*log(drand())) * cos(2*M_PI*drand());
}
int main()
{
  int i;
  double rands[1000];
  for (i=0; i<1000; i++)
    rands[i] = 1.0 + 0.5*random_normal();
  return 0;
}











Ada: with Ada.Numerics;                       use Ada.Numerics;
with Ada.Numerics.Float_Random;          use Ada.Numerics.Float_Random;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;

procedure Normal_Random is
   function Normal_Distribution
            (  Seed  : Generator;
               Mu    : Float := 1.0;
               Sigma : Float := 0.5
            )  return Float is 
   begin
      return
         Mu + (Sigma * Sqrt (-2.0 * Log (Random (Seed), 10.0)) * Cos (2.0 * Pi * Random (Seed)));
   end Normal_Distribution;
      
   Seed         : Generator;
   Distribution : array (1..1_000) of Float; 
begin
   Reset (Seed);
   for I in Distribution'Range loop
      Distribution (I) := Normal_Distribution (Seed);
   end loop;
end Normal_Random;
Task: Random numbers


C++: #include <random>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
  random_device seed;
  mt19937 engine(seed());
  normal_distribution<double> dist(1.0, 0.5);
  auto rnd = bind(dist, engine);

  vector<double> v(1000);
  generate(v.begin(), v.end(), rnd);
  return 0;
}










Ada: with Ada.Numerics;                       use Ada.Numerics;
with Ada.Numerics.Float_Random;          use Ada.Numerics.Float_Random;
with Ada.Numerics.Elementary_Functions;  use Ada.Numerics.Elementary_Functions;

procedure Normal_Random is
   function Normal_Distribution
            (  Seed  : Generator;
               Mu    : Float := 1.0;
               Sigma : Float := 0.5
            )  return Float is 
   begin
      return
         Mu + (Sigma * Sqrt (-2.0 * Log (Random (Seed), 10.0)) * Cos (2.0 * Pi * Random (Seed)));
   end Normal_Distribution;
      
   Seed         : Generator;
   Distribution : array (1..1_000) of Float; 
begin
   Reset (Seed);
   for I in Distribution'Range loop
      Distribution (I) := Normal_Distribution (Seed);
   end loop;
end Normal_Random;