1/*2* Licensed to the Apache Software Foundation (ASF) under one3* or more contributor license agreements. See the NOTICE file4* distributed with this work for additional information5* regarding copyright ownership. The ASF licenses this file6* to you under the Apache License, Version 2.0 (the7* "License"); you may not use this file except in compliance8* with the License. You may obtain a copy of the License at9*10* http://www.apache.org/licenses/LICENSE-2.011*12* Unless required by applicable law or agreed to in writing, software13* distributed under the License is distributed on an "AS IS" BASIS,14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15* See the License for the specific language governing permissions and16* limitations under the License.17*/18packageorg.apache.giraph.writable.kryo; 19 20importjava.util.Random; 21 22/**23* Transient Random class. Seed/state is not kept after24* serializing/deserializing.25*26* Within Blocks Framework - if we initialize Random within the Piece, when27* it's serialzied and copied to all workers and all threads - keeping seed28* would cause same series of random numbers to be generated everywhere.29*30* So this class is safe to be used in Pieces, while using regular Random31* class is forbidden to be serialized.32* Best approach would be to not have Random serialized, and create it on33* workers, where possible.34*/35publicclassTransientRandom { 36/**Instance of random object */37privatefinaltransientRandom random =newRandom(); 38 39/**40* Get instance of Random41* @return Random instance42*/43publicRandom get() { 44returnrandom; 45 } 46 47/**48* Returns a pseudorandom, uniformly distributed {@code int} value49* between 0 (inclusive) and the specified value (exclusive), drawn from50* this random number generator's sequence.51*52* @param n Given upper limit53* @return pseudorandom integer number in [0, n) range.54*/55publicintnextInt(intn) { 56returnrandom.nextInt(n); 57 } 58 59/**60* Returns the next pseudorandom, uniformly distributed61* {@code double} value between {@code 0.0} and62* {@code 1.0} from this random number generator's sequence.63*64* @return pseudorandom number in [0, 1)65*/66publicdoublenextDouble() { 67returnrandom.nextDouble(); 68 } 69 }