001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math3.random; 018 019import java.util.Random; 020import org.apache.commons.math3.exception.NotStrictlyPositiveException; 021 022/** 023 * Utilities for creating {@link RandomGenerator} instances. 024 * 025 * @since 3.3 026 */ 027public class RandomGeneratorFactory { 028 /** 029 * Class contains only static methods. 030 */ 031 private RandomGeneratorFactory() {} 032 033 /** 034 * Creates a {@link RandomDataGenerator} instance that wraps a 035 * {@link Random} instance. 036 * 037 * @param rng JDK {@link Random} instance that will generate the 038 * the random data. 039 * @return the given RNG, wrapped in a {@link RandomGenerator}. 040 */ 041 public static RandomGenerator createRandomGenerator(final Random rng) { 042 return new RandomGenerator() { 043 /** {@inheritDoc} */ 044 public void setSeed(int seed) { 045 rng.setSeed((long) seed); 046 } 047 048 /** {@inheritDoc} */ 049 public void setSeed(int[] seed) { 050 rng.setSeed(convertToLong(seed)); 051 } 052 053 /** {@inheritDoc} */ 054 public void setSeed(long seed) { 055 rng.setSeed(seed); 056 } 057 058 /** {@inheritDoc} */ 059 public void nextBytes(byte[] bytes) { 060 rng.nextBytes(bytes); 061 } 062 063 /** {@inheritDoc} */ 064 public int nextInt() { 065 return rng.nextInt(); 066 } 067 068 /** {@inheritDoc} */ 069 public int nextInt(int n) { 070 if (n <= 0) { 071 throw new NotStrictlyPositiveException(n); 072 } 073 return rng.nextInt(n); 074 } 075 076 /** {@inheritDoc} */ 077 public long nextLong() { 078 return rng.nextLong(); 079 } 080 081 /** {@inheritDoc} */ 082 public boolean nextBoolean() { 083 return rng.nextBoolean(); 084 } 085 086 /** {@inheritDoc} */ 087 public float nextFloat() { 088 return rng.nextFloat(); 089 } 090 091 /** {@inheritDoc} */ 092 public double nextDouble() { 093 return rng.nextDouble(); 094 } 095 096 /** {@inheritDoc} */ 097 public double nextGaussian() { 098 return rng.nextGaussian(); 099 } 100 }; 101 } 102 103 /** 104 * Converts seed from one representation to another. 105 * 106 * @param seed Original seed. 107 * @return the converted seed. 108 */ 109 public static long convertToLong(int[] seed) { 110 // The following number is the largest prime that fits 111 // in 32 bits (i.e. 2^32 - 5). 112 final long prime = 4294967291l; 113 114 long combined = 0l; 115 for (int s : seed) { 116 combined = combined * prime + s; 117 } 118 119 return combined; 120 } 121}