Random Numbers

DSP, Plug-in and Host development discussion.
evm
KVRist
102 posts since 14 Jul, 2008

Post Thu Aug 12, 2010 5:18 am

Hi

Code: Select all

 srand(1234);
for(int i=10;;i--)
int r = rand()%10+1; 
Will the same set of pseudo random numbers be produced regardless
of Intel or AMD CPU used on MSVC2008. I only have intel.

Regards
Etric

KVRAF
6237 posts since 12 Feb, 2006 from Helsinki, Finland

Post Thu Aug 12, 2010 5:25 am

Almost certainly yes for different processors. Will the same set of numbers be produced when you switch between different compilers? Not necessarily.

Just don't expect any sort of quality from standard C library rand() though. Typically it's something simple like a linear congruent generator, and for consistent results you might just as well implement your own... though I have some trouble imagining why you'd want consistent results from it anyway?
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

Skunk Mod
21283 posts since 10 Jun, 2004 from Pony Pasture

Post Thu Aug 12, 2010 5:33 am

mystran wrote:though I have some trouble imagining why you'd want consistent results from it anyway?
Repeatable "random" (pseudo-) sample-and-hold effect comes to mind.

KVRAF
6237 posts since 12 Feb, 2006 from Helsinki, Finland

Post Thu Aug 12, 2010 5:58 am

Meffy wrote:
mystran wrote:though I have some trouble imagining why you'd want consistent results from it anyway?
Repeatable "random" (pseudo-) sample-and-hold effect comes to mind.
Oh ok, I guess I'm too purist to think such things should be repeatable. :D
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

evm
KVRist

Topic Starter

102 posts since 14 Jul, 2008

Post Thu Aug 12, 2010 6:13 am

mystran wrote:...though I have some trouble imagining why you'd want consistent results from it anyway?
It is part of a physical modelling algorithm.
I need to produce a consistent set of random numbers based
on a initial seed. If differnet CPUs produce different results
the model will drift inside the delay, altering the sound dramatically.
I think a more fixed/hard coded approach is needed.

Regards
Etric

Skunk Mod
21283 posts since 10 Jun, 2004 from Pony Pasture

Post Thu Aug 12, 2010 6:13 am

@mystran: I'm reminded of the clash of world-views in Ursula LeGuin's "Always Coming Home." The Kesh, a more or less rural far-future people living in what used to be northern California, tend to believe in not recording, not freezing in time, what has been. They prefer to experience what they are near, then let that pass on with the flow of time. They reject photography, music recording, and such.

@evm: That makes sense.

KVRian
1101 posts since 2 Oct, 2001 from Berlin, Germany

Post Thu Aug 12, 2010 6:56 am

What about multiple LFOs?
Just tune these to a dividable prima ratio (2:3:5:7...) and take the average on each sampling. There'll be quantization issues which are directly connected to the number of LFOs. Depending on what you want, this can be either quite nice, or not.

Hm... perhaps more suitable for you:
http://www.musicdsp.org/archive.php?classid=1#59
Sascha Eversmeier
drummer of The Board
software dev in the studio-speaker biz | former plugin creator [u-he, samplitude & digitalfishphones]

Mr Entertainment
12209 posts since 30 Apr, 2002 from i might peeramid

Post Thu Aug 12, 2010 9:05 am

write your own rnd function :p the musicdsp one is elementary enough and can easily be embellished. all you need to do then is ensure the variable bit depth.
you come and go, you come and go. amitabha neither a follower nor a leader be tagore "where roads are made i lose my way" where there is certainty, consideration is absent.

evm
KVRist

Topic Starter

102 posts since 14 Jul, 2008

Post Thu Aug 12, 2010 9:52 am

Yes, thank you, I can see an algorithm from music DSP as my future to avoid
using rand() altogether.

Regards
Etric

Mr Entertainment
12209 posts since 30 Apr, 2002 from i might peeramid

Post Thu Aug 12, 2010 10:02 am

cheers :) i'll have a GDI .sem source pls for that helpful tip :hihi:

if you're new at noise, search for perlin noise/value noise (there are/were? some nice walkthrus of this around) if you want to get fancy.. essentially it's like additive synthesis.. combining 'octaves' of rnd vals, use some curvy interpolation for lower octaves and reduce the amplitude of higher octaves by 1/n.. this makes 'nice' noise :p

for rnd vals, i generally use the straight alg.. another layer modifying the first may be prudent and is low cpu if you're not using it on every sample.
you come and go, you come and go. amitabha neither a follower nor a leader be tagore "where roads are made i lose my way" where there is certainty, consideration is absent.

KVRian
909 posts since 1 Dec, 2003 from tejas

Post Fri Aug 13, 2010 12:22 pm

Code: Select all

#define  ku32A    0x19660D       // from Numerical Recipes
#define  ku32C    0x3C6EF35F     // a prime near (sqrt(5) - 2) * M ( 2^32 here)

unsigned long gdwAcc; // global 32-bit running accumulator

unsigned long NextLCGen()
{
   return (gdwAcc = (gdwAcc * ku32A) + ku32C); 
}
make sure you use the upper bits of the accumulator - the lower bits of a linear congruential generator tend to be far less "random"

peace y'all !

KVRian
501 posts since 30 Jan, 2009 from UK

Post Fri Aug 13, 2010 1:40 pm

Another option is to just look at the source for rand / srand - for more info:

GCC rand
http://qa.coreboot.org/docs/libpayload/ ... ource.html

MSVC rand
http://stackoverflow.com/questions/1931 ... d-function
Matt
www.liquidsonics.com
Follow us on Facebook

KVRAF
6237 posts since 12 Feb, 2006 from Helsinki, Finland

Post Sat Aug 14, 2010 6:22 am

http://www.number.com.pt/index.html I like this one for floats. :)
Preferred pronouns would be "it/it" because according to this country, I'm a piece of human trash.

KVRer
11 posts since 17 Dec, 2008 from Sweden, Jönköping

Post Mon Aug 16, 2010 2:44 am

Perfect white noise with LFSR( http://en.wikipedia.org/wiki/Linear_fee ... t_register )
usage of lfsr has been used before (maybe still) to encrypt satellite tv channels or any kind of data on embedded systems.

Found this a while back on musicdsp.org that I frequently use.

// init
unsigned int sr0=0x67452301;
unsigned int sr1=0xefcdab89;

//run
unsigned int ret=sr1;
sr0^=sr1;
sr1+=sr0;
return ret;

.. it is a beuty. :)

Return to “DSP and Plug-in Development”