From db464f35f5a10b58d9ed1085e0b462689adee583 Mon Sep 17 00:00:00 2001 From: Siddhesh Wani Date: Mon, 25 May 2015 14:46:31 +0530 Subject: Original Version --- src/c/auxiliaryFunctions/rand/drands.c | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/c/auxiliaryFunctions/rand/drands.c (limited to 'src/c/auxiliaryFunctions/rand/drands.c') diff --git a/src/c/auxiliaryFunctions/rand/drands.c b/src/c/auxiliaryFunctions/rand/drands.c new file mode 100644 index 00000000..9b7c4cf4 --- /dev/null +++ b/src/c/auxiliaryFunctions/rand/drands.c @@ -0,0 +1,71 @@ +/* + * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab + * Copyright (C) 2008 - INRIA - Arnaud TORSET + * + * This file must be used under the terms of the CeCILL. + * This source file is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at + * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt + * + */ + + +/* + URAND, A UNIVERSAL RANDOM NUMBER GENERATOR + BY, MICHAEL A. MALCOLM, CLEVE B. MOLER, + STAN-CS-73-334, JANUARY 1973, + COMPUTER SCIENCE DEPARTMENT, + School of Humanities and Sciences, STANFORD UNIVERSITY, + ftp://reports.stanford.edu/pub/cstr/reports/cs/tr/73/334/CS-TR-73-334.pdf + +*/ + + + +#include "rand.h" +#include + +double drands(void) { + int m=1; + const int itwo=2; + static int m2=0,halfm=0,ia=0, ic=0, mic=0, iy=0; + static double s=0.0; + + if (m2==0){ + /* if first entry, compute machine integer word length */ + while (m>m2){ + m2=m; + m=itwo*m2; + } + halfm = m2; + + /* compute multiplier and increment for linear congruential method */ + ia = 8*(int)(halfm*atan(1.0)/8.0) + 5; + ic = 2*(int)(halfm*(0.5-sqrt(3.0)/6.0)) + 1; + mic = (m2 - ic) + m2; + + /* s is the scale factor for converting to floating point */ + s = 0.5/halfm; + } + + /* compute next random number */ + iy = iy*ia; + + /* the following statement is for computers which do not allow + integer overflow on addition */ + if (iy > mic) iy = (iy - m2) - m2; + + iy = iy + ic; + + /* the following statement is for computers where the + word length for addition is greater than for multiplication */ + if (iy/2 > m2) iy = (iy - m2) - m2; + + /* the following statement is for computers where integer + overflow affects the sign bit */ + if (iy < 0) iy = (iy + m2) + m2; + return (double)iy*s; +} + + -- cgit From ee5bf4d90f78673d1e4d5a9dd304ee53c503c085 Mon Sep 17 00:00:00 2001 From: imushir Date: Tue, 9 Feb 2016 16:38:27 +0530 Subject: Support for Servo motor for Arduino. Support for'diag' in c --- src/c/auxiliaryFunctions/rand/drands.c | 43 ++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'src/c/auxiliaryFunctions/rand/drands.c') diff --git a/src/c/auxiliaryFunctions/rand/drands.c b/src/c/auxiliaryFunctions/rand/drands.c index 9b7c4cf4..658cd0b0 100644 --- a/src/c/auxiliaryFunctions/rand/drands.c +++ b/src/c/auxiliaryFunctions/rand/drands.c @@ -34,37 +34,60 @@ double drands(void) { if (m2==0){ /* if first entry, compute machine integer word length */ - while (m>m2){ + while (m>m2){ m2=m; m=itwo*m2; + } + halfm = m2; - - /* compute multiplier and increment for linear congruential method */ + + /* compute multiplier and increment for linear congruential method */ ia = 8*(int)(halfm*atan(1.0)/8.0) + 5; - ic = 2*(int)(halfm*(0.5-sqrt(3.0)/6.0)) + 1; - mic = (m2 - ic) + m2; - - /* s is the scale factor for converting to floating point */ + + ic = 2*(int)(halfm*(0.5-sqrt(3.0)/6.0)) + 1; + + mic = (m2 - ic) + m2; + + /* s is the scale factor for converting to floating point */ s = 0.5/halfm; + + } /* compute next random number */ iy = iy*ia; + + /* the following statement is for computers which do not allow integer overflow on addition */ - if (iy > mic) iy = (iy - m2) - m2; + if (iy > mic) + { + iy = (iy - m2) - m2; + + } iy = iy + ic; /* the following statement is for computers where the word length for addition is greater than for multiplication */ - if (iy/2 > m2) iy = (iy - m2) - m2; + if (iy/2 > m2) + { + iy = (iy - m2) - m2; + + } /* the following statement is for computers where integer overflow affects the sign bit */ - if (iy < 0) iy = (iy + m2) + m2; + if (iy < 0) + { + iy = (iy + m2) + m2; + + + } + + return (double)iy*s; } -- cgit