Csound Csound-dev Csound-tekno Search About

[Csnd] Range of random number generators

Date2022-01-18 10:44
FromTetsuya Miwa
Subject[Csnd] Range of random number generators
Hi all, 

I thought all the random number generators having range parameter in Csound return the number in closed range (a <= N <= b, [a,b]).
However, I started to feel less confident about it after checking the manual.
For example, the manual says rnd(x) returns a random number in the unipolar range 0 to x.
Is it [0,x] or [0,x) (semi-open)?

Tetsuya Miwa
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

Date2022-01-18 11:41
FromShane Byrne
SubjectRe: [Csnd] Range of random number generators
Hi Tetsuya,

The opcode random will allow for declaration of minimum and maximum values at i-time, k-rate or audio rate.

Maybe that's what you're looking for?

Shane.

On Tue, Jan 18, 2022 at 10:44 AM Tetsuya Miwa <izc07036@nifty.com> wrote:
Hi all,

I thought all the random number generators having range parameter in Csound return the number in closed range (a <= N <= b, [a,b]).
However, I started to feel less confident about it after checking the manual.
For example, the manual says rnd(x) returns a random number in the unipolar range 0 to x.
Is it [0,x] or [0,x) (semi-open)?

Tetsuya Miwa
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

Date2022-01-18 19:37
FromPete Goodeve
SubjectRe: [Csnd] Range of random number generators
AttachmentsNone  

Date2022-01-18 20:57
Fromjoachim heintz
SubjectRe: [Csnd] Range of random number generators
well just by trial ...

instr Test
  indx = 1
  while indx < 10000000 do
   iRnd = rnd:i(1)
   if iRnd > .999999 then
    printf_i "rnd(1) = %f in cycle %d.\n", indx, iRnd, indx
   endif
  indx += 1
  od
endin
schedule("Test",0,0)

... i get this:

rnd(1) = 0.999999 in cycle 4820758.
rnd(1) = 1.000000 in cycle 6319223.
rnd(1) = 0.999999 in cycle 7789384.
rnd(1) = 1.000000 in cycle 9287849.

in other words: it takes some time, but it is not impossible.

	j




On 18/01/2022 20:37, Pete Goodeve wrote:
> On Tue, Jan 18, 2022 at 07:44:22PM +0900, Tetsuya Miwa wrote:
>> Hi all,
>>
>> I thought all the random number generators having range parameter in Csound return the number in closed range (a <= N <= b, [a,b]).
>> However, I started to feel less confident about it after checking the manual.
>> For example, the manual says rnd(x) returns a random number in the unipolar range 0 to x.
>> Is it [0,x] or [0,x) (semi-open)?
>>
> Not sure of your notation there, and one of the developers will have
> to give the definitive answer, but as far as I know 'rnd(10)'  can include
> the exact output '10', not just "arbitrarily close, but never exactly...".
> 
> However, ift is really random, it might be hard to tell!
> 
> 	-- Pete --
> 
> 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

Date2022-01-18 21:21
FromPete Goodeve
SubjectRe: [Csnd] Range of random number generators
AttachmentsNone  

Date2022-01-18 21:34
FromEduardo Moguillansky
SubjectRe: [Csnd] Range of random number generators
What you see is just printf rounding due to %f being limited to 6 comma 
places. Try:

instr Test
  indx = 1
  while indx < 1000000000 do
   iRnd = rnd:i(1)
   if iRnd > .9999998 then
    printf_i "rnd(1) = %.12f in cycle %d.\n", indx, iRnd, indx
   endif
  indx += 1
  od
endin



On 18.01.22 22:21, Pete Goodeve  wrote:
 > I guess that clears up the question, then (:-))
 > Thanks for checking!
 >
 >     -- Pete--
 >
 > On Tue, Jan 18, 2022 at 09:57:48PM +0100, joachim heintz wrote:
 > > well just by trial ...
 > >
 > > instr Test
 > >   indx = 1
 > >   while indx < 10000000 do
 > >    iRnd = rnd:i(1)
 > >    if iRnd > .999999 then
 > >     printf_i "rnd(1) = %f in cycle %d.\n", indx, iRnd, indx
 > >    endif
 > >   indx += 1
 > >   od
 > > endin
 > > schedule("Test",0,0)
 > >
 > > ... i get this:
 > >
 > > rnd(1) = 0.999999 in cycle 4820758.
 > > rnd(1) = 1.000000 in cycle 6319223.
 > > rnd(1) = 0.999999 in cycle 7789384.
 > > rnd(1) = 1.000000 in cycle 9287849.
 > >
 > > in other words: it takes some time, but it is not impossible.
 > >
 > >     j
 > >
 > >
 > >
 > >
 > > On 18/01/2022 20:37, Pete Goodeve wrote:
 > >> On Tue, Jan 18, 2022 at 07:44:22PM +0900, Tetsuya Miwa wrote:
 > >>> Hi all,
 > >>>
 > >>> I thought all the random number generators having range parameter in
 > >>> Csound return the number in closed range (a <= N <= b, [a,b]).
 > >>> However, I started to feel less confident about it after checking the
 > >>> manual.
 > >>> For example, the manual says rnd(x) returns a random number in the
 > >>> unipolar range 0 to x.
 > >>> Is it [0,x] or [0,x) (semi-open)?
 > >>>
 > >> Not sure of your notation there, and one of the developers will have
 > >> to give the definitive answer, but as far as I know 'rnd(10)'  can 
include
 > >> the exact output '10', not just "arbitrarily close, but never 
exactly...".
 > >>
 > >> However, ift is really random, it might be hard to tell!
 > >>
 > >>     -- Pete --
 > >>
 > >> 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
 >
 > 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
 >
 >

On 18.01.22 21:57, joachim heintz wrote:
> well just by trial ...
>
> instr Test
>  indx = 1
>  while indx < 10000000 do
>   iRnd = rnd:i(1)
>   if iRnd > .999999 then
>    printf_i "rnd(1) = %f in cycle %d.\n", indx, iRnd, indx
>   endif
>  indx += 1
>  od
> endin
> schedule("Test",0,0)
>
> ... i get this:
>
> rnd(1) = 0.999999 in cycle 4820758.
> rnd(1) = 1.000000 in cycle 6319223.
> rnd(1) = 0.999999 in cycle 7789384.
> rnd(1) = 1.000000 in cycle 9287849.
>
> in other words: it takes some time, but it is not impossible.
>
>     j
>
>
>
>
> On 18/01/2022 20:37, Pete Goodeve wrote:
>> On Tue, Jan 18, 2022 at 07:44:22PM +0900, Tetsuya Miwa wrote:
>>> Hi all,
>>>
>>> I thought all the random number generators having range parameter in 
>>> Csound return the number in closed range (a <= N <= b, [a,b]).
>>> However, I started to feel less confident about it after checking 
>>> the manual.
>>> For example, the manual says rnd(x) returns a random number in the 
>>> unipolar range 0 to x.
>>> Is it [0,x] or [0,x) (semi-open)?
>>>
>> Not sure of your notation there, and one of the developers will have
>> to give the definitive answer, but as far as I know 'rnd(10)' can 
>> include
>> the exact output '10', not just "arbitrarily close, but never 
>> exactly...".
>>
>> However, ift is really random, it might be hard to tell!
>>
>>     -- Pete --
>>
>> 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

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

Date2022-01-18 21:49
Fromjoachim heintz
SubjectRe: [Csnd] Range of random number generators
good point =)

so it seems that it never gets to the upper limit.  probably it uses the 
rand() in C which is defined this way?

On 18/01/2022 22:34, Eduardo Moguillansky wrote:
> What you see is just printf rounding due to %f being limited to 6 comma 
> places. Try:
> 
> instr Test
>   indx = 1
>   while indx < 1000000000 do
>    iRnd = rnd:i(1)
>    if iRnd > .9999998 then
>     printf_i "rnd(1) = %.12f in cycle %d.\n", indx, iRnd, indx
>    endif
>   indx += 1
>   od
> endin
> 
> 
> 
> On 18.01.22 22:21, Pete Goodeve  wrote:
>  > I guess that clears up the question, then (:-))
>  > Thanks for checking!
>  >
>  >     -- Pete--
>  >
>  > On Tue, Jan 18, 2022 at 09:57:48PM +0100, joachim heintz wrote:
>  > > well just by trial ...
>  > >
>  > > instr Test
>  > >   indx = 1
>  > >   while indx < 10000000 do
>  > >    iRnd = rnd:i(1)
>  > >    if iRnd > .999999 then
>  > >     printf_i "rnd(1) = %f in cycle %d.\n", indx, iRnd, indx
>  > >    endif
>  > >   indx += 1
>  > >   od
>  > > endin
>  > > schedule("Test",0,0)
>  > >
>  > > ... i get this:
>  > >
>  > > rnd(1) = 0.999999 in cycle 4820758.
>  > > rnd(1) = 1.000000 in cycle 6319223.
>  > > rnd(1) = 0.999999 in cycle 7789384.
>  > > rnd(1) = 1.000000 in cycle 9287849.
>  > >
>  > > in other words: it takes some time, but it is not impossible.
>  > >
>  > >     j
>  > >
>  > >
>  > >
>  > >
>  > > On 18/01/2022 20:37, Pete Goodeve wrote:
>  > >> On Tue, Jan 18, 2022 at 07:44:22PM +0900, Tetsuya Miwa wrote:
>  > >>> Hi all,
>  > >>>
>  > >>> I thought all the random number generators having range parameter in
>  > >>> Csound return the number in closed range (a <= N <= b, [a,b]).
>  > >>> However, I started to feel less confident about it after checking 
> the
>  > >>> manual.
>  > >>> For example, the manual says rnd(x) returns a random number in the
>  > >>> unipolar range 0 to x.
>  > >>> Is it [0,x] or [0,x) (semi-open)?
>  > >>>
>  > >> Not sure of your notation there, and one of the developers will have
>  > >> to give the definitive answer, but as far as I know 'rnd(10)'  can 
> include
>  > >> the exact output '10', not just "arbitrarily close, but never 
> exactly...".
>  > >>
>  > >> However, ift is really random, it might be hard to tell!
>  > >>
>  > >>     -- Pete --
>  > >>
>  > >> 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
>  >
>  > 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
>  >
>  >
> 
> On 18.01.22 21:57, joachim heintz wrote:
>> well just by trial ...
>>
>> instr Test
>>  indx = 1
>>  while indx < 10000000 do
>>   iRnd = rnd:i(1)
>>   if iRnd > .999999 then
>>    printf_i "rnd(1) = %f in cycle %d.\n", indx, iRnd, indx
>>   endif
>>  indx += 1
>>  od
>> endin
>> schedule("Test",0,0)
>>
>> ... i get this:
>>
>> rnd(1) = 0.999999 in cycle 4820758.
>> rnd(1) = 1.000000 in cycle 6319223.
>> rnd(1) = 0.999999 in cycle 7789384.
>> rnd(1) = 1.000000 in cycle 9287849.
>>
>> in other words: it takes some time, but it is not impossible.
>>
>>     j
>>
>>
>>
>>
>> On 18/01/2022 20:37, Pete Goodeve wrote:
>>> On Tue, Jan 18, 2022 at 07:44:22PM +0900, Tetsuya Miwa wrote:
>>>> Hi all,
>>>>
>>>> I thought all the random number generators having range parameter in 
>>>> Csound return the number in closed range (a <= N <= b, [a,b]).
>>>> However, I started to feel less confident about it after checking 
>>>> the manual.
>>>> For example, the manual says rnd(x) returns a random number in the 
>>>> unipolar range 0 to x.
>>>> Is it [0,x] or [0,x) (semi-open)?
>>>>
>>> Not sure of your notation there, and one of the developers will have
>>> to give the definitive answer, but as far as I know 'rnd(10)' can 
>>> include
>>> the exact output '10', not just "arbitrarily close, but never 
>>> exactly...".
>>>
>>> However, ift is really random, it might be hard to tell!
>>>
>>>     -- Pete --
>>>
>>> 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
> 
> 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

Date2022-01-19 11:46
FromTetsuya Miwa
SubjectRe: [Csnd] Range of random number generators
I know this is a trivial question and does not affect practical use of random number generators. I am just curious.
If rnd(x) can return x, the code below can cause error though the probability is extremely low.

kArr[] fillarray 10,20,30
kindex = int(rnd:k(3))
kdata = kArr[kindex]

I found following description for ‘rand’ in the Csound manual. There’s no such description for the other random generators.
> The value ares or kres is within is a half-closed interval which contains -xamp, but never contains +xamp. 

Regarding other programming languages, all the below functions returns 0<= x <1 (semi-open). I’m not sure about C.
Microsoft Visual Basic: Rnd()
Python: random()
Java: java.lang.Math.random()

Tetsuya Miwa
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

Date2022-01-19 17:25
Fromjohn
SubjectRe: [Csnd] Range of random number generators
On Liux rand is clear

The  rand()  function returns a pseudo-random integer in the range 0 to
        RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).

although my C standard text does not give any guidance

In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit 
generator but the"new" familty is based on a 31ut generator.  Elsewhere 
Csound uses the C rand function and ten converts to range and type.

I have not investiagated the properties of the 31bit generator but it is

  * Code adapted from Ray Garder's public domain code of July 1997 at:
  * http://www.snippets.org/RG_RAND.C     Thanks!
  *
  *  Based on "Random Number Generators: Good Ones Are Hard to Find",
  *  S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
  *  and "Two Fast Implementations of the 'Minimal Standard' Random
  *  Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), p. 
87-88
  *
  *  Linear congruential generator: f(z) = (16807 * z) mod (2 ** 31 - 1)
  *
  *  Uses L. Schrage's method to avoid overflow problems.


On Wed, 19 Jan 2022, Tetsuya Miwa wrote:

> I know this is a trivial question and does not affect practical use of random number generators. I am just curious.
> If rnd(x) can return x, the code below can cause error though the probability is extremely low.
>
> kArr[] fillarray 10,20,30
> kindex = int(rnd:k(3))
> kdata = kArr[kindex]
>
> I found following description for ‘rand’ in the Csound manual. There’s no such description for the other random generators.
>> The value ares or kres is within is a half-closed interval which contains -xamp, but never contains +xamp.
>
> Regarding other programming languages, all the below functions returns 0<= x <1 (semi-open). I’m not sure about C.
> Microsoft Visual Basic: Rnd()
> Python: random()
> Java: java.lang.Math.random()
>
> Tetsuya Miwa
> 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

Date2022-01-20 12:12
FromTetsuya Miwa
SubjectRe: [Csnd] Range of random number generators
Dear John,

Thank you for your clarification!

> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.


If that's the case, the point is how C rand function is converted to the specified range.
I will check the source cord though I’m not a C programmer...

Tetsuya Miwa

> 2022/01/20 2:25、john のメール:
> 
> On Liux rand is clear
> 
> The  rand()  function returns a pseudo-random integer in the range 0 to
>       RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).
> 
> although my C standard text does not give any guidance
> 
> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.
> 
> I have not investiagated the properties of the 31bit generator but it is
> 
> * Code adapted from Ray Garder's public domain code of July 1997 at:
> * http://www.snippets.org/RG_RAND.C     Thanks!
> *
> *  Based on "Random Number Generators: Good Ones Are Hard to Find",
> *  S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
> *  and "Two Fast Implementations of the 'Minimal Standard' Random
> *  Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), p. 87-88
> *
> *  Linear congruential generator: f(z) = (16807 * z) mod (2 ** 31 - 1)
> *
> *  Uses L. Schrage's method to avoid overflow problems.
> 
> 
> On Wed, 19 Jan 2022, Tetsuya Miwa wrote:
> 
>> I know this is a trivial question and does not affect practical use of random number generators. I am just curious.
>> If rnd(x) can return x, the code below can cause error though the probability is extremely low.
>> 
>> kArr[] fillarray 10,20,30
>> kindex = int(rnd:k(3))
>> kdata = kArr[kindex]
>> 
>> I found following description for ‘rand’ in the Csound manual. There’s no such description for the other random generators.
>>> The value ares or kres is within is a half-closed interval which contains -xamp, but never contains +xamp.
>> 
>> Regarding other programming languages, all the below functions returns 0<= x <1 (semi-open). I’m not sure about C.
>> Microsoft Visual Basic: Rnd()
>> Python: random()
>> Java: java.lang.Math.random()
>> 
>> Tetsuya Miwa
>> 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

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

Date2022-01-20 13:00
Fromjoachim heintz
SubjectRe: [Csnd] Range of random number generators
hi tetsuya -

just to note that for the case you mentioned: getting a random element 
from an array, i always subtract something like 0.0001 from the maximum. 
  this will not ruin the randomness, but let me be sure that no element 
out of the range will be indexed.  something like:

opcode ArrRndEl, i, i[]oj
  iInArr[], iStart, iEnd xin
  iLen lenarray iInArr
  iEnd = (iEnd == -1) ? iLen-1 : iEnd
  iElIndx random iStart, iEnd+0.999
  iEl = iInArr[int(iElIndx)]
  xout iEl
endop

best -
	joachim


On 20/01/2022 13:12, Tetsuya Miwa wrote:
> Dear John,
> 
> Thank you for your clarification!
> 
>> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.
> 
> 
> If that's the case, the point is how C rand function is converted to the specified range.
> I will check the source cord though I’m not a C programmer...
> 
> Tetsuya Miwa
> 
>> 2022/01/20 2:25、john のメール:
>>
>> On Liux rand is clear
>>
>> The  rand()  function returns a pseudo-random integer in the range 0 to
>>        RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).
>>
>> although my C standard text does not give any guidance
>>
>> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.
>>
>> I have not investiagated the properties of the 31bit generator but it is
>>
>> * Code adapted from Ray Garder's public domain code of July 1997 at:
>> * http://www.snippets.org/RG_RAND.C     Thanks!
>> *
>> *  Based on "Random Number Generators: Good Ones Are Hard to Find",
>> *  S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
>> *  and "Two Fast Implementations of the 'Minimal Standard' Random
>> *  Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), p. 87-88
>> *
>> *  Linear congruential generator: f(z) = (16807 * z) mod (2 ** 31 - 1)
>> *
>> *  Uses L. Schrage's method to avoid overflow problems.
>>
>>
>> On Wed, 19 Jan 2022, Tetsuya Miwa wrote:
>>
>>> I know this is a trivial question and does not affect practical use of random number generators. I am just curious.
>>> If rnd(x) can return x, the code below can cause error though the probability is extremely low.
>>>
>>> kArr[] fillarray 10,20,30
>>> kindex = int(rnd:k(3))
>>> kdata = kArr[kindex]
>>>
>>> I found following description for ‘rand’ in the Csound manual. There’s no such description for the other random generators.
>>>> The value ares or kres is within is a half-closed interval which contains -xamp, but never contains +xamp.
>>>
>>> Regarding other programming languages, all the below functions returns 0<= x <1 (semi-open). I’m not sure about C.
>>> Microsoft Visual Basic: Rnd()
>>> Python: random()
>>> Java: java.lang.Math.random()
>>>
>>> Tetsuya Miwa
>>> 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
> 
> 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

Date2022-01-20 15:17
FromTetsuya Miwa
SubjectRe: [Csnd] Range of random number generators
Dear Joachim,

Thank you for sharing your code. Yes, it is a simple solution, bit inelegant though.

Tetsuya Miwa

> 2022/01/20 22:00、joachim heintz のメール:
> 
> hi tetsuya -
> 
> just to note that for the case you mentioned: getting a random element from an array, i always subtract something like 0.0001 from the maximum.  this will not ruin the randomness, but let me be sure that no element out of the range will be indexed.  something like:
> 
> opcode ArrRndEl, i, i[]oj
> iInArr[], iStart, iEnd xin
> iLen lenarray iInArr
> iEnd = (iEnd == -1) ? iLen-1 : iEnd
> iElIndx random iStart, iEnd+0.999
> iEl = iInArr[int(iElIndx)]
> xout iEl
> endop
> 
> best -
> 	joachim
> 
> 
> On 20/01/2022 13:12, Tetsuya Miwa wrote:
>> Dear John,
>> Thank you for your clarification!
>>> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.
>> If that's the case, the point is how C rand function is converted to the specified range.
>> I will check the source cord though I’m not a C programmer...
>> Tetsuya Miwa
>>> 2022/01/20 2:25、john のメール:
>>> 
>>> On Liux rand is clear
>>> 
>>> The  rand()  function returns a pseudo-random integer in the range 0 to
>>>       RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).
>>> 
>>> although my C standard text does not give any guidance
>>> 
>>> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.
>>> 
>>> I have not investiagated the properties of the 31bit generator but it is
>>> 
>>> * Code adapted from Ray Garder's public domain code of July 1997 at:
>>> * http://www.snippets.org/RG_RAND.C     Thanks!
>>> *
>>> *  Based on "Random Number Generators: Good Ones Are Hard to Find",
>>> *  S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
>>> *  and "Two Fast Implementations of the 'Minimal Standard' Random
>>> *  Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), p. 87-88
>>> *
>>> *  Linear congruential generator: f(z) = (16807 * z) mod (2 ** 31 - 1)
>>> *
>>> *  Uses L. Schrage's method to avoid overflow problems.
>>> 
>>> 
>>> On Wed, 19 Jan 2022, Tetsuya Miwa wrote:
>>> 
>>>> I know this is a trivial question and does not affect practical use of random number generators. I am just curious.
>>>> If rnd(x) can return x, the code below can cause error though the probability is extremely low.
>>>> 
>>>> kArr[] fillarray 10,20,30
>>>> kindex = int(rnd:k(3))
>>>> kdata = kArr[kindex]
>>>> 
>>>> I found following description for ‘rand’ in the Csound manual. There’s no such description for the other random generators.
>>>>> The value ares or kres is within is a half-closed interval which contains -xamp, but never contains +xamp.
>>>> 
>>>> Regarding other programming languages, all the below functions returns 0<= x <1 (semi-open). I’m not sure about C.
>>>> Microsoft Visual Basic: Rnd()
>>>> Python: random()
>>>> Java: java.lang.Math.random()
>>>> 
>>>> Tetsuya Miwa
>>>> 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
>> 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

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

Date2022-01-20 22:18
FromPartev Sarkissian <0000060b2ef1338e-dmarc-request@LISTSERV.HEANET.IE>
SubjectRe: [Csnd] Range of random number generators

Yoi desu.


-PBS


-----Original Message-----
From: Tetsuya Miwa <izc07036@NIFTY.COM>
To: CSOUND@LISTSERV.HEANET.IE
Sent: Thu, Jan 20, 2022 7:17 am
Subject: Re: [Csnd] Range of random number generators

Dear Joachim,

Thank you for sharing your code. Yes, it is a simple solution, bit inelegant though.

Tetsuya Miwa

> 2022/01/20 22:00、joachim heintz <jh@JOACHIMHEINTZ.DE>のメール:
>
> hi tetsuya -
>
> just to note that for the case you mentioned: getting a random element from an array, i always subtract something like 0.0001 from the maximum.  this will not ruin the randomness, but let me be sure that no element out of the range will be indexed.  something like:
>
> opcode ArrRndEl, i, i[]oj
> iInArr[], iStart, iEnd xin
> iLen lenarray iInArr
> iEnd = (iEnd == -1) ? iLen-1 : iEnd
> iElIndx random iStart, iEnd+0.999
> iEl = iInArr[int(iElIndx)]
> xout iEl
> endop
>
> best -
>     joachim
>
>
> On 20/01/2022 13:12, Tetsuya Miwa wrote:
>> Dear John,
>> Thank you for your clarification!
>>> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.
>> If that's the case, the point is how C rand function is converted to the specified range.
>> I will check the source cord though I’m not a C programmer...
>> Tetsuya Miwa
>>> 2022/01/20 2:25、john <jpff@CODEMIST.CO.UK>のメール:
>>>
>>> On Liux rand is clear
>>>
>>> The  rand()  function returns a pseudo-random integer in the range 0 to
>>>      RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).
>>>
>>> although my C standard text does not give any guidance
>>>
>>> In Csound tereare three different PSNGs; Tne rigial isbased on a 16 bit generator but the"new" familty is based on a 31ut generator.  Elsewhere Csound uses the C rand function and ten converts to range and type.
>>>
>>> I have not investiagated the properties of the 31bit generator but it is
>>>
>>> * Code adapted from Ray Garder's public domain code of July 1997 at:
>>> * http://www.snippets.org/RG_RAND.C     Thanks!
>>> *
>>> *  Based on "Random Number Generators: Good Ones Are Hard to Find",
>>> *  S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
>>> *  and "Two Fast Implementations of the 'Minimal Standard' Random
>>> *  Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), p. 87-88
>>> *
>>> *  Linear congruential generator: f(z) = (16807 * z) mod (2 ** 31 - 1)
>>> *
>>> *  Uses L. Schrage's method to avoid overflow problems.
>>>
>>>
>>> On Wed, 19 Jan 2022, Tetsuya Miwa wrote:
>>>
>>>> I know this is a trivial question and does not affect practical use of random number generators. I am just curious.
>>>> If rnd(x) can return x, the code below can cause error though the probability is extremely low.
>>>>
>>>> kArr[] fillarray 10,20,30
>>>> kindex = int(rnd:k(3))
>>>> kdata = kArr[kindex]
>>>>
>>>> I found following description for ‘rand’ in the Csound manual. There’s no such description for the other random generators.
>>>>> The value ares or kres is within is a half-closed interval which contains -xamp, but never contains +xamp.
>>>>
>>>> Regarding other programming languages, all the below functions returns 0<= x <1 (semi-open). I’m not sure about C.
>>>> Microsoft Visual Basic: Rnd()
>>>> Python: random()
>>>> Java: java.lang.Math.random()
>>>>
>>>> Tetsuya Miwa
>>>> 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
>> 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

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