home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (random.groovy)

This example Groovy source code file (random.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

ia, ia, ic, ic, im, im

The Groovy random.groovy source code

/*
	The Computer Language Shootout
	http://shootout.alioth.debian.org/

	contributed by Jochen Hinrichsen
*/

def IM = 139968
def IA = 3877
def IC = 29573
def last = 42D

def gen_random(Double max) {
	last = (last * IA + IC) % IM
	max * last / IM
}

def n = (args.length == 0 ? 1 : args[0].toInteger()) - 1
while (n--) {
	gen_random(100D)
}

// TODO groovy does not support varargs
// def s = new java.io.PrintStream(System.out)
// s.printf("%.9f", gen_random(100D))

def nf = java.text.NumberFormat.getInstance()
nf.setMaximumFractionDigits(9)
nf.setMinimumFractionDigits(9)
nf.setGroupingUsed(false)
println nf.format(gen_random(100D))

// EOF

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy random.groovy source code file:

new blog posts

 

Copyright 1998-2013 Alvin Alexander, alvinalexander.com
All Rights Reserved.