Csound Csound-dev Csound-tekno Search About

Random Log vs Random Exp

Date2016-05-02 11:26
FromPeter Burgess
SubjectRandom Log vs Random Exp
Hi there. I have a question that doesn't relate to Csound as such, but
is of the nature that Csounders might be able to help me with it.

I have created a list of different random number generators over the
last year for my app. Two types of these are called RandomExp() and
RandomLog(). These, as you might expect, give a random number with
exponential and logarythmic distributions. But, I am struggling to
figure out which distribution is which.

RandomLog2() is currently the one I use for generating things like
random frequencies. It gives a distribution that favours lower
numbers, and thus gives an even octave spread between the min and max
values that I pass to it. Due to it's favouring of lower numbers, I
feel like it's distribution is logarithmic, hence why I called it
RandomLog, however, the results it gives are suitable for generating
numbers that are interpreted in an exponential fashion.

Is my perception of the distribution of the numbers generated wrong?
Should I maybe switch the names so that RandomExp2() is suited to
random frequency generation? Certainly, whenever I go to generate a
random frequency, I automatically use RandomExp2() instead of
RandomLog2() and get temporarily baffled by the results, so clearly in
my head they should be the opposite way round.

It is a small deal really, but I want to get this right, as this is a
good list of functions that I am now using as a standard utility for
other applications I am writing too.

Pete

Date2016-05-02 11:40
FromPablo Frank
SubjectRe: Random Log vs Random Exp
can you paste the relevant code?

> Date: Mon, 2 May 2016 11:26:59 +0100
> From: pete.soundtechnician@GMAIL.COM
> Subject: [Csnd] Random Log vs Random Exp
> To: CSOUND@LISTSERV.HEANET.IE
>
> Hi there. I have a question that doesn't relate to Csound as such, but
> is of the nature that Csounders might be able to help me with it.
>
> I have created a list of different random number generators over the
> last year for my app. Two types of these are called RandomExp() and
> RandomLog(). These, as you might expect, give a random number with
> exponential and logarythmic distributions. But, I am struggling to
> figure out which distribution is which.
>
> RandomLog2() is currently the one I use for generating things like
> random frequencies. It gives a distribution that favours lower
> numbers, and thus gives an even octave spread between the min and max
> values that I pass to it. Due to it's favouring of lower numbers, I
> feel like it's distribution is logarithmic, hence why I called it
> RandomLog, however, the results it gives are suitable for generating
> numbers that are interpreted in an exponential fashion.
>
> Is my perception of the distribution of the numbers generated wrong?
> Should I maybe switch the names so that RandomExp2() is suited to
> random frequency generation? Certainly, whenever I go to generate a
> random frequency, I automatically use RandomExp2() instead of
> RandomLog2() and get temporarily baffled by the results, so clearly in
> my head they should be the opposite way round.
>
> It is a small deal really, but I want to get this right, as this is a
> good list of functions that I am now using as a standard utility for
> other applications I am writing too.
>
> Pete
>
> --
> http://algorythmradio.com
> https://soundcloud.com/algorythmradio
>
> Csound mailing list
> Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
> Send bugs reports to
> https://github.com/csound/csound/issues
> Discussions of bugs and features can be posted here

Date2016-05-02 12:48
FromPeter Burgess
SubjectRe: Random Log vs Random Exp
Sure can. As I said, this question isn't entirely Csound related, so
it's  c++ function. Bellow is what I'm currently using to randomly
generate frequencies, which favours smaller numbers. log2() and exp2()
are functions from the math.h, and RandomDouble(min, max) is any
random number generator with linear distribution.

double RandomDoubleLogTwo (double minResult, double maxResult) {

    double minLog = log2 ((double)minResult);
    double maxLog = log2 ((double)maxResult + 1);
    double randomLog = RandomDouble (minLog, maxLog);
    double result = exp2 (randomLog);

    return result;
}

The function bellow is the opposite which favours big numbers. It also
has a safety measure when using a number range that returns extremely
large results from exp2();

double RandomDoubleExpTwo (double minResult, double maxResult) {

    // Make sure minExp & maxExp don't exceed max double value
    unsigned char resultFactors10 = 0;
    while (maxResult > 1000) {
        minResult /= 10;
        maxResult /= 10;
        resultFactors10++;
    }

    double minExp = exp2 (minResult);
    double maxExp = exp2 (maxResult);
    double randomExp = RandomDouble (minExp, maxExp);
    double result = log2 (randomExp);

    while (resultFactors10 > 0) {
        result *= 10;
        resultFactors10--;
    }

    return result;
}


What do you think, have I got the names backwards? Or is it rright to
use a logarithmic distribution to find a number that is used in an
exponential fassion?

On Mon, May 2, 2016 at 11:40 AM, Pablo Frank  wrote:
> can you paste the relevant code?
>
>> Date: Mon, 2 May 2016 11:26:59 +0100
>> From: pete.soundtechnician@GMAIL.COM
>> Subject: [Csnd] Random Log vs Random Exp
>> To: CSOUND@LISTSERV.HEANET.IE
>
>>
>> Hi there. I have a question that doesn't relate to Csound as such, but
>> is of the nature that Csounders might be able to help me with it.
>>
>> I have created a list of different random number generators over the
>> last year for my app. Two types of these are called RandomExp() and
>> RandomLog(). These, as you might expect, give a random number with
>> exponential and logarythmic distributions. But, I am struggling to
>> figure out which distribution is which.
>>
>> RandomLog2() is currently the one I use for generating things like
>> random frequencies. It gives a distribution that favours lower
>> numbers, and thus gives an even octave spread between the min and max
>> values that I pass to it. Due to it's favouring of lower numbers, I
>> feel like it's distribution is logarithmic, hence why I called it
>> RandomLog, however, the results it gives are suitable for generating
>> numbers that are interpreted in an exponential fashion.
>>
>> Is my perception of the distribution of the numbers generated wrong?
>> Should I maybe switch the names so that RandomExp2() is suited to
>> random frequency generation? Certainly, whenever I go to generate a
>> random frequency, I automatically use RandomExp2() instead of
>> RandomLog2() and get temporarily baffled by the results, so clearly in
>> my head they should be the opposite way round.
>>
>> It is a small deal really, but I want to get this right, as this is a
>> good list of functions that I am now using as a standard utility for
>> other applications I am writing too.
>>
>> Pete
>>
>> --
>> http://algorythmradio.com
>> https://soundcloud.com/algorythmradio
>>
>> Csound mailing list
>> Csound@listserv.heanet.ie
>> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>> Send bugs reports to
>> https://github.com/csound/csound/issues
>> Discussions of bugs and features can be posted here
> Csound mailing list Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features can
> be posted here