rnd

rnd — Returns a random number in a unipolar range at the rate given by the input argument.

Description

Returns a random number in a unipolar range at the rate given by the input argument.

Syntax

rnd(x) (init- or control-rate only)

Where the argument within the parentheses may be an expression. These value converters sample a global random sequence, but do not reference seed. The result can be a term in a further expression.

Performance

Returns a random number in the unipolar range 0 to x.

Examples

Here is an example of the rnd opcode. Play rnd.csd

Example 916. Example of the rnd opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in
-odac           -iadc    ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o rnd.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

; Andres Cabrera 2010

sr = 44100
ksmps = 4410
nchnls = 1
0dbfs = 1

instr 1
	; Generate a random number from 0 to 10.
	irand = rnd(10)
	print irand
endin

instr 2
	klimit init 10
	krand = rnd(klimit)
	printk 0, krand
endin

</CsInstruments>
<CsScore>

i 1 0 1  ; Generate 1 number
i 1 0 1  ; Generate another number
i 1 0 1  ; yet another number

i 2 2 1  ; 1 second prints 9 values (kr = 10)
e

</CsScore>
</CsoundSynthesizer>


Its output should be:

SECTION 1:
new alloc for instr 1:
instr 1:  irand = 9.735
new alloc for instr 1:
instr 1:  irand = 1.394
new alloc for instr 1:
instr 1:  irand = 7.695
midi channel 1 now using instr 1
B  0.000 ..  2.000 T  2.000 TT  2.000 M:  0.00000
new alloc for instr 2:
 i   2 time     2.10000:     5.25005
 i   2 time     2.20000:     6.22665
 i   2 time     2.30000:     9.69511
 i   2 time     2.40000:     7.16822
 i   2 time     2.50000:     9.45134
 i   2 time     2.60000:     1.34123
 i   2 time     2.70000:     2.09879
 i   2 time     2.80000:     2.36001
 i   2 time     2.90000:     0.03553

Here is another example of the rnd opcode. Play rnd-advanced.csd

Example 917. Advanced example of the rnd opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in
-odac           -iadc    ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o rnd.wav -W ;;; for file output any platform

; By Stefano Cucchi 2020

</CsOptions>
<CsInstruments>


sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1
; Generate a random number from 0 to 5
irand1 = rnd(5)
; Generate a random number from 0 to 13
irand2 = rnd(13)

print irand1
print irand2

a1, a2 crossfm 200, 250, irand1, irand2, 1, 1, 1
kdeclick linseg 0, 0.2, 0.5, p3-0.4, 0.5, 0.2, 0

outch 1, a1*kdeclick
outch 2, a2*kdeclick

endin

</CsInstruments>
<CsScore>

f 1 0 4096 10 1 0 1 0 0.5 0 0.2

i 1 0 1  
i 1 + 1  
i 1 + 1 
i 1 + 1 


e

</CsScore>
</CsoundSynthesizer>


See Also

birnd

Credits

Author: Barry L. Vercoe
MIT
Cambridge, Massachussetts
1997