diff options
Diffstat (limited to 'src/com/jsyn/util/PseudoRandom.java')
-rw-r--r-- | src/com/jsyn/util/PseudoRandom.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/com/jsyn/util/PseudoRandom.java b/src/com/jsyn/util/PseudoRandom.java new file mode 100644 index 0000000..b7f619c --- /dev/null +++ b/src/com/jsyn/util/PseudoRandom.java @@ -0,0 +1,89 @@ +/* + * Copyright 2009 Phil Burk, Mobileer Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Sep 9, 2009 + * com.jsyn.engine.units.SynthRandom.java + */ + +package com.jsyn.util; + +import java.util.Random; + +/** + * Pseudo-random numbers using predictable and fast linear-congruential method. + * + * @author Phil Burk (C) 2009 Mobileer Inc Translated from 'C' to Java by Lisa + * Tolenti. + */ +public class PseudoRandom { + // We must shift 1L or else we get a negative number! + private static final double INT_TO_DOUBLE = (1.0 / (1L << 31)); + private long seed = 99887766; + + /** + * Create an instance of SynthRandom. + */ + public PseudoRandom() { + this(new Random().nextInt()); + } + + /** + * Create an instance of PseudoRandom. + */ + public PseudoRandom(int seed) { + setSeed(seed); + } + + public void setSeed(int seed) { + this.seed = ((long) seed) & 0xFFFFFFFF; + } + + public int getSeed() { + return (int) seed; + } + + /** + * Returns the next random double from 0.0 to 1.0 + * + * @return value from 0.0 to 1.0 + */ + public double random() { + int positiveInt = nextRandomInteger() & 0x7FFFFFFF; + return positiveInt * INT_TO_DOUBLE; + } + + /** + * Returns the next random double from -1.0 to 1.0 + * + * @return value from -1.0 to 1.0 + */ + public double nextRandomDouble() { + return nextRandomInteger() * INT_TO_DOUBLE; + } + + /** Calculate random 32 bit number using linear-congruential method. */ + public int nextRandomInteger() { + long rand = (seed * 196314165) + 907633515; + seed = rand & 0x00000000FFFFFFFFL; + return (int) rand; + } + + public int choose(int range) { + long bigRandom = nextRandomInteger() & 0x7FFFFFFF; + long temp = bigRandom * range; + return (int) (temp >> 31); + } +} |