Nice TWiki > Doc > CodeExamples > RandomBeginnerExample (r1.2) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the BeginnerExamples
To compile:
   nicec --sourcepath=.. -a random.jar random
To run:
   java -jar random.jar 900000
*/


import java.text.*;

void main(String[] args){
   var n = toSingleInt(args);
   double result = 0.0;

   while (n-- > 0) result = gen_random(100.0);

   println(floatFormat(9).format(result));
}


let int IM = 139968;
let int IA = 3877;
let int IC = 29573;
var int seed = 42;


double gen_random(double max) {        
   seed = (seed * IA + IC) % IM;
   return( max * seed / IM );
}


NumberFormat floatFormat(int digits){
   let f = NumberFormat.getInstance();
   f.setGroupingUsed(false);
   f.setMaximumFractionDigits(digits);
   f.setMinimumFractionDigits(digits);
   return f;
} 


int toSingleInt(String[] s){
   try { return Integer.parseInt(s[0]); }
   catch (Exception e){ return 1; } }


/* Notes - language
Compare with Java at the "Win32 Language Shootout"

   import java.text.*;
Import the Java package java.text. 

   let int IM = 139968;
Define package constant IM

   var int seed = 42;
Define package variable seed

   double gen_random(double max) {
Define package function gen_random

   NumberFormat floatFormat(int digits){
Define package function floatFormat
*/
-- IsaacGouy - 27 Aug 2003

Topic RandomBeginnerExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.2 - 27 Aug 2003 - 16:33 GMT - IsaacGouy
Parents: WebHome > CodeExamples
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.

Doc.RandomBeginnerExample moved from Doc.RandomNoviceExample on 27 Aug 2003 - 16:31 by IsaacGouy - put it back