Nice TWiki > Doc > RandomBeginnerExample (r1.1 vs. r1.3) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
 <<O>>  Difference Topic RandomBeginnerExample (r1.3 - 10 Jan 2004 - IsaacGouy)

 <<O>>  Difference Topic RandomBeginnerExample (r1.2 - 27 Aug 2003 - IsaacGouy)
Changed:
<
<

/* Please try the BeginnerExamples? before the NoviceExamples?

>
>

/* Please try the NoviceExamples? before the BeginnerExamples?

Added:
>
>

%META:TOPICMOVED{by="IsaacGouy" date="1062001901" from="Doc.RandomNoviceExample" to="Doc.RandomBeginnerExample"}%


 <<O>>  Difference Topic RandomBeginnerExample (r1.1 - 27 Aug 2003 - IsaacGouy)
Added:
>
>

%META:TOPICINFO{author="IsaacGouy" date="1061944865" format="1.0" version="1.1"}% %META:TOPICPARENT{name="CodeExamples"}%

/* Please try the BeginnerExamples before the NoviceExamples
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 . { View | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.1 - 27 Aug 2003 - 00:41 GMT - IsaacGouy
Revision r1.3 - 10 Jan 2004 - 15:42 GMT - IsaacGouy
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.