[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
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

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.

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
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

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

I guess that clears up the question, then (:-))
Thanks for checking!

  -- Pete--

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

> I guess that clears up the question, then (:-))
> Thanks for checking!
>
> -- Pete--
>
> > 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
> >
> >>> 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
> >> LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
> >> Send bugs reports to
> >> Issues · csound/csound · GitHub
> >> Discussions of bugs and features can be posted here
> >
> > Csound mailing list
> > Csound@listserv.heanet.ie
> > LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
> > Send bugs reports to
> > Issues · csound/csound · GitHub
> > Discussions of bugs and features can be posted here
>
> Csound mailing list
> Csound@listserv.heanet.ie
> LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
> Send bugs reports to
> Issues · csound/csound · GitHub
> Discussions of bugs and features can be posted here
>

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

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
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
Issues · csound/csound · GitHub
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
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

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?

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
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

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!

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.

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
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
      Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
      Issues · csound/csound · GitHub
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
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

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

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

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.

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
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
      Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
      Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Csound mailing list
Csound@listserv.heanet.ie
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
      Issues · csound/csound · GitHub
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
        Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here

Yoi desu.

-PBS