Question: I’d like to build a script(s) that randomly choose a number between 1 and 16 every N triggers but importantly once a number has been selected it is never selected again. Is this possible with the TT?

Thanks in advance for any thoughts on this.

Ps. I’m new to programming the TT, slowly working through the Studies and reading other people’s scripts and breaking them down.

Could you implement it in such a way so that as you run the random op script, it inserts this number into a pattern? Every time you then call for a random number, it will continue to check if it exists in the pattern, if not, then insert into pattern and return. If so, run itself to some large iteration limit.

You could get fancy and store the numbers as bits in a variable so as to economize on pattern space.

2 Likes

The question is what happens when you ask for the 17th random number and all 16 have been used?

I might do something like this:

EVERY 17: Y 0
X RND 15
W BGET Y X: X % + 1 X 15
Y BSET X
X + 1 X

(Am I remembering right that BSET/BGET are 0-based? If not, this script needs some minor adjustments.)

Alternately, you could use a similar method in the init script (or called manually) to fill in the first 16 entries of a pattern, and then loop through the pattern.

1 Like

If you can spare the last lane of pattern mode to hold your random values, you could do it with the destructive-to-pattern-length ops:

T RRAND 0 PN.L 3; X PN 3 T
PN.RM 3 T
IF PN.L 3: BREAK
L 0 15: PN.PUSH 3 I

…leaving your random value in X. The list of values will repopulate whenever it’s empty.

4 Likes

Thanks for the ideas, I’ll work through the suggested scripts & see how I get on, then I’ll report back!

Cheers

I slightly modified your script to output the random number as a note value to the ER301 (see below) and these seems to work pretty much as expected, except that I’m getting 0 values output (which aren’t in the range of 1 to 16) and these occur quite frequently within a run of the 16 numbers.

For example: 12, 4, 10, 15, 3, 14, 0, 1, 7, 0, 8, 11, 0, 4, 12, 9, 0, 2, 0, 5, 0, 6, 13

Any ideas why that might be? I’ve tried to understand your script using the manual and I understand parts of it but not all of it so I’m unable at the moment to see why the 0’s are occurring. Appreciate your help with this.

T RRAND 0 PN.L 3; X PN 3 T
PN.RM 3 T
SC.CV 1 Y; Y N X
IF PN.L 3: BREAK
L 0 15: PN.PUSH 3 I

This script script outputs in the range 0-15 (it’s easier for a computer to count from 0), so if you want to have them 1-16 just add one…

EDIT: Actually, if I understand your question, the output you posted doesn’t fulfill the requirements, I would expect a list of exactly 16 numbers, each appearing exactly once…

1 Like

I tried adding 1 to the resulting X value & that did change the output from 0 to 1 but more importantly, as you pointed out, the script as is doesn’t actually work exactly as i hoped, just very close it what i want.

I didn’t try it, but @Starthief suggestion should work…

One problem with my approach is that patterns are zero-indexed: the first slot is slot 0, the second is slot 1, and so on. There’s a fix, but let me break the whole thing down line by line.

T RRAND 0 PN.L 3; X PN 3 T

This puts our random number in T and puts the pattern value at the corresponding slot into X. The range on RRAND here is from 0 to the current length of the pattern, inclusive, but recall that our patterns are zero-indexed; if we happen to roll the maximum value here (the pattern length), we’ll be trying to get a pattern slot that’s actually one position past the end of the pattern. My bad! I’ll give a fixed line when we’re done.

Anyway, the second clause here after the semicolon is putting the value that’s currently at slot T in the pattern into X. The pattern only contains numbers we haven’t picked out already this round, so this is the actual return value we want. The next step is to remove the value we just picked from the pattern so we don’t pick it again until after a refill:

PN.RM 3 T

So that’s done, removing the value and pulling the rest of the pattern up to fill the empty space, thereby changing the pattern length. If the pattern still has any length, that’s all we want to do here, so it’s time for my favorite trick, the conditional break:

IF PN.L 3: BREAK

PN.L 3 will be zero if we’re out of values; if it’s anything else, the BREAK will execute and we exit the script. If we’re still here by line 4, though, the pattern must be empty, so it’s time to refill.

L 0 15: PN.PUSH 3 I

This loops through its bounds (inclusive of both of them, just like RRAND), putting the loop number into I, and pushing I onto the pattern, automagically extending the pattern length to fit. But here’s what got me: PUSH doesn’t take a position as an argument - it doesn’t need to. It just pushes onto the end, it’s in the name. So I didn’t need to zero-index this loop at all. We could just add 1 to I and fix our delivered values to properly run from 1 to 16, we’ve got room on the line after all… but running the loop starting at zero is totally unnecessary. (As is zero indexing, by the way; computers don’t get any real advantage from it anymore, it’s just an onion in the varnish. Alas, we’re probably stuck with it.)

Here’s the complete script with fixes.

T RRAND 0 - 1 PN.L 3; X PN 3 T
PN.RM 3 T
IF PN.L 3: BREAK
L 1 16: PN.PUSH 3 I

Line 1 is a tight squeeze, but still fits. Phew!

5 Likes

Many many thanks for the very thorough break down, this is immediately helpful as well as providing a revision! Hopefully I can try this evening when I return from work.

@a773 & @Starthief - I’m also going to try your suggestions too as I like to understand an alternative approach just to learn more about how to solve problems in the TT!

Thanks, everyone!

@misuba just tried your revised script, works like dream!!! Added bonus is that it self resets too. Many thanks for the help, I’ll go with this approach for now. Thanks again

2 Likes

I’m redoing studies 1-3 for the nth time (did I use that right?). It’s just now opening my mind slowly to what to make of it, what it all means, feel an oddly renewed sense of purpose in the process. Like tinkering with music and data and wanted to thank everyone again for making this happen.

5 Likes

hi. I’m just starting out learning the key functions. I’m sure I’m missing something obvious I can’t get emulate grid press, alt-space, to work. Tried it in regular and full grid visualizer but not luck. No problem with moving cursor or selecting grid area. What am I missing?

TT3.0.0, core vortex keyboard.

hmm, that’s weird. it must be something keyboard specific. does the space work when in full view mode? (you don’t need to use alt in full mode).

It doesn’t work in full view mode either.

but it works when entering scripts and in live mode?

edit: do you have any buttons or faders defined? the press only works on defined controls, it won’t do anything on an empty grid.

1 Like

yeah it works entering scripts. That’s gotta be it; no defined controls…it is an empty grid. Thanks for getting me unstuck.

if you haven’t seen the grid studies yet check them out: https://github.com/scanner-darkly/teletype/wiki/GRID-INTEGRATION

1 Like

I’ll definitely check those out. Thanks.