yading@11: /* yading@11: * Lagged Fibonacci PRNG yading@11: * Copyright (c) 2008 Michael Niedermayer yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #ifndef AVUTIL_LFG_H yading@11: #define AVUTIL_LFG_H yading@11: yading@11: typedef struct AVLFG { yading@11: unsigned int state[64]; yading@11: int index; yading@11: } AVLFG; yading@11: yading@11: void av_lfg_init(AVLFG *c, unsigned int seed); yading@11: yading@11: /** yading@11: * Get the next random unsigned 32-bit number using an ALFG. yading@11: * yading@11: * Please also consider a simple LCG like state= state*1664525+1013904223, yading@11: * it may be good enough and faster for your specific use case. yading@11: */ yading@11: static inline unsigned int av_lfg_get(AVLFG *c){ yading@11: c->state[c->index & 63] = c->state[(c->index-24) & 63] + c->state[(c->index-55) & 63]; yading@11: return c->state[c->index++ & 63]; yading@11: } yading@11: yading@11: /** yading@11: * Get the next random unsigned 32-bit number using a MLFG. yading@11: * yading@11: * Please also consider av_lfg_get() above, it is faster. yading@11: */ yading@11: static inline unsigned int av_mlfg_get(AVLFG *c){ yading@11: unsigned int a= c->state[(c->index-55) & 63]; yading@11: unsigned int b= c->state[(c->index-24) & 63]; yading@11: return c->state[c->index++ & 63] = 2*a*b+a+b; yading@11: } yading@11: yading@11: /** yading@11: * Get the next two numbers generated by a Box-Muller Gaussian yading@11: * generator using the random numbers issued by lfg. yading@11: * yading@11: * @param out array where the two generated numbers are placed yading@11: */ yading@11: void av_bmg_get(AVLFG *lfg, double out[2]); yading@11: yading@11: #endif /* AVUTIL_LFG_H */