random

Full source code

C++

// -*- mode: c++ -*-
// $Id$
// http://www.bagley.org/~doug/shootout/

#include <iostream>
#include <stdlib.h>
#include <math.h>

using namespace std;

#define IM 139968
#define IA 3877
#define IC 29573

inline double gen_random(double max) {
    static long last = 42;
    last = (last * IA + IC) % IM;
    return( max * last / IM );
}

int main(int argc, char *argv[]) {
#ifdef SMALL_PROBLEM_SIZE
#define LENGTH 4000000
#else
#define LENGTH 400000000
#endif
    int N = ((argc == 2) ? atoi(argv[1]) : LENGTH);
    double result = 0;
    
    while (N--) {
        result = gen_random(100.0);
    }
    cout.precision(9);
    cout.setf(ios::fixed);
    cout << result << endl;
    return(0);
}

Rust

// Adapted from https://github.com/llvm/llvm-test-suite and
// http://www.bagley.org/~doug/shootout/
use std::env;

#[cfg(feature = "small_problem_size")]
const LENGTH: i32 = 4000000;

#[cfg(not(feature = "small_problem_size"))]
const LENGTH: i32 = 400000000;

const IM: i64 = 139968;
const IA: i64 = 3877;
const IC: i64 = 29573;

fn gen_random(last_random: &mut i64, max: f64) -> f64 {
    let new_last = (*last_random * IA + IC) % IM;
    *last_random = new_last;
    return max * (new_last as f64) / (IM as f64);
}

fn main() {
    let mut args = env::args();
    let mut n = if args.len() == 2 {
        args.nth(1).unwrap().parse::<i32>().unwrap()
    } else {
        LENGTH
    };

    let mut result: f64 = 0.0;
    let mut last_random: i64 = 42;

    while n != 0 {
        n -= 1;
        result = gen_random(&mut last_random, 100.0);
    }

    println!("{:.9}", result);
}

Porting notes

The gen_random function is the same as the one in heapsort.