From 93f64df7a4eee1151d2b70d37ab17f04e58cbc46 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 13 Dec 2010 01:37:22 +0000 Subject: refactored x_misc.c into cputime.c loadband.c namecanvas.c realtime.c random.c svn path=/trunk/; revision=14600 --- externals/vanilla/random.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 externals/vanilla/random.c (limited to 'externals/vanilla/random.c') diff --git a/externals/vanilla/random.c b/externals/vanilla/random.c new file mode 100644 index 00000000..67e4cf35 --- /dev/null +++ b/externals/vanilla/random.c @@ -0,0 +1,75 @@ +/* Copyright (c) 1997-1999 Miller Puckette. +* For information on usage and redistribution, and for a DISCLAIMER OF ALL +* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ + +/* misc. */ + +#include "m_pd.h" +#include "s_stuff.h" +#include +#include +#include +#ifdef UNISTD +#include +#include +#include +#include +#include +#endif +#ifdef MSW +#include +#include +#endif + +static t_class *random_class; + +typedef struct _random +{ + t_object x_obj; + t_float x_f; + unsigned int x_state; +} t_random; + + +static int makeseed(void) +{ + static unsigned int random_nextseed = 1489853723; + random_nextseed = random_nextseed * 435898247 + 938284287; + return (random_nextseed & 0x7fffffff); +} + +static void *random_new(t_floatarg f) +{ + t_random *x = (t_random *)pd_new(random_class); + x->x_f = f; + x->x_state = makeseed(); + floatinlet_new(&x->x_obj, &x->x_f); + outlet_new(&x->x_obj, &s_float); + return (x); +} + +static void random_bang(t_random *x) +{ + int n = x->x_f, nval; + int range = (n < 1 ? 1 : n); + unsigned int randval = x->x_state; + x->x_state = randval = randval * 472940017 + 832416023; + nval = ((double)range) * ((double)randval) + * (1./4294967296.); + if (nval >= range) nval = range-1; + outlet_float(x->x_obj.ob_outlet, nval); +} + +static void random_seed(t_random *x, t_float f, t_float glob) +{ + x->x_state = f; +} + +static void random_setup(void) +{ + random_class = class_new(gensym("random"), (t_newmethod)random_new, 0, + sizeof(t_random), 0, A_DEFFLOAT, 0); + class_addbang(random_class, random_bang); + class_addmethod(random_class, (t_method)random_seed, + gensym("seed"), A_FLOAT, 0); +} -- cgit v1.2.1