Random Numbers

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

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

Post

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?

Post

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.

Post

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

Post

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

Post

@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.

Post

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]

Post

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.

Post

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

Regards
Etric

Post

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.

Post

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 !

Post

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

Post

http://www.number.com.pt/index.html I like this one for floats. :)

Post

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. :)

Post Reply

Return to “DSP and Plugin Development”