SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Digits

Spend every digit of a random number

Say you need two random choices: a digit from 0 to 9, and a number from 0 to 99.

The natural thing is to draw twice from your generator and take what you need from each draw.

cell  contents  notes
 10      050    INP 50  cells 10-31: the seed
 11      150    CLA 50  cells 11-17: the next random number, s = (21s + 17) mod 1000
 12      250    ADD 50
 13      422    SFT 22
 14      410    SFT 10
 15      250    ADD 50
 16      251    ADD 51
 17      650    STO 50
 18      433    SFT 33  a digit, 0-9, from the first draw
 19      652    STO 52
 20      150    CLA 50  cells 20-26: the next random number, s = (21s + 17) mod 1000
 21      250    ADD 50
 22      422    SFT 22
 23      410    SFT 10
 24      250    ADD 50
 25      251    ADD 51
 26      650    STO 50
 27      422    SFT 22  a number, 0-99, from a second draw
 28      653    STO 53
 29      552    OUT 52
 30      553    OUT 53
 31      900    HRS 00
 50      000
 51      017
 52      000
 53      000

Step through the natural way in Cardigan

But a three-digit random number has three digits in it, and you only need three. Draw once, take the digit from the end and the 0 to 99 from the front.

cell  contents  notes
 10      050    INP 50  cells 10-25: the seed
 11      150    CLA 50  cells 11-17: the next random number, s = (21s + 17) mod 1000
 12      250    ADD 50
 13      422    SFT 22
 14      410    SFT 10
 15      250    ADD 50
 16      251    ADD 51
 17      650    STO 50
 18      433    SFT 33  a digit, 0-9: the last digit
 19      652    STO 52
 20      150    CLA 50
 21      412    SFT 12  a number, 0-99: the first two digits, from the same draw
 22      653    STO 53
 23      552    OUT 52
 24      553    OUT 53
 25      900    HRS 00
 50      000
 51      017
 52      000
 53      000

Step through the CARDIAC way in Cardigan

From the seed 472, drawing twice prints 9 and 26, and drawing once prints 9 and 92. Two draws take 26 cells and run 22 instructions; one draw takes 20 cells and runs 16.

From the tips in cardiac-code on GitHub. All the tips.