Some questions about array methods

I have two issues with generating pattern with array.

(
~funcB = { arg len = 16;
	var n = Array.fill(len, {arg i ; i});
	var m = Array.fill(rrand(4, len-1), {n.choose}).sort;
	var randNum = rrand(0, len);

	m.postln;

};
)

~funcB.();
  1. I would like to make an array with only one number in an array. is there any methods to do that?
(
~funcC = { arg len = 16;
	var n = Array.fill(len, {arg i ; if ([true,false].choose, {i}, {0})}).sort;
	n.postln;

};
)
  1. which method will be works removing specific item in array? I would like to remove in all “0” in this array

for #2 use the method .reject

[\a, \b, \c].reject{|i| i == \b} // [\a, \c]
1 Like

You create an array of one item using exactly the same methods you would use to create an array of multiple items, except with only one item.

[1, 2, 3]  // ok

[3]  // ok

Array.fill(3, { 10.rand })  // ok

Array.fill(1, { 10.rand })  // ok

// etc...

hjh

I miss typed the question. number 0 to 16, I want to get a only one number at once for a set of [0…16] I would like to array like [2, 3, 6, 12] or [2, 4, 5, 11, 12,15] not [2, 2, 3, 4, 5, 5, 5]

and thank you so much you solve so many my weird question :slight_smile:

Perhaps see:

Set.fill(5) { 10.rand }

If the collection of interest is small and you want all answers to be of the same size perhaps:

"of-interest".scramble.keep(5)

Ah OK… sorry for misunderstanding.

.scramble.keep(num) is nice – I’ve used that myself.

.take is another nice method.

// note: 0..16 contains 17 numbers;
// I'll assume that's not what you meant
a = (0..15);
Array.fill(5, { a.take(a.choose) });
-> [ 4, 10, 1, 15, 7 ]

// note: 0..16 contains 17 numbers;
// I'll assume that's not what you meant
a = (0..15);
Array.fill(5, { a.take(a.choose) });
-> [ 4, 10, 1, 15, 7 ]

// or
b = Bag.with(*(0..15));
Array.fill(5, { b.take });  // no need to .choose

hjh

And If I get an array [2, 3, 5, 6, 13], is there any method to check specific values in the array? like python method?
for example, I would like to condition statment, like If or switch after generating the array!

Like which python method?

Because the desired operation isn’t specified, I’m kinda guessing here, but SuperCollider has .includes, for starters.

hjh