![見出し画像](https://assets.st-note.com/production/uploads/images/129882681/rectangle_large_type_2_7bf7641fc792366e6b71f96f763a183d.png?width=1200)
【ティラノビルダー】10個の変数の値から、2つの値をランダムに取り出す方法
![](https://assets.st-note.com/production/uploads/images/129888981/picture_pc_a1dbd47019568ee86be866db1dc1710f.gif?width=1200)
ティラノビルダーで10個の変数の値から、2つの値をランダムに取り出す方法について解説します。
①変数の追加
![](https://assets.st-note.com/img/1707107660397-qt2vySVxIq.png?width=1200)
「プロジェクト」→「変数管理」をクリックし、変数管理画面を開きます。
![](https://assets.st-note.com/img/1707108222395-YXEo98hUAs.png?width=1200)
name1~name10
result
上記の変数を追加します。
変数name1~name10の初期値はお好みの値を入力してください。
変数resultの初期値は空欄でOKです。
これで、変数の追加は完了です。
②ラベルコンポーネントの配置
![](https://assets.st-note.com/img/1707108933531-iivVSyBz29.png?width=1200)
button
get
上記のラベルコンポーネントを配置します。
③分岐ボタンコンポーネントの配置
![](https://assets.st-note.com/img/1707109261067-JaSBDbQfRf.png?width=1200)
分岐ボタンコンポーネントを配置します。
ターゲットにラベルgetを指定します。
④停止コンポーネントの配置
![](https://assets.st-note.com/img/1707109370067-yxC1eEBbjT.png?width=1200)
停止コンポーネントを配置します。
⑤iscriptコンポーネントの配置
![](https://assets.st-note.com/img/1707109567032-eCbja9syBx.png?width=1200)
iscriptコンポーネントを配置します。
![](https://assets.st-note.com/img/1707109622577-V7vmrF038K.png?width=1200)
下記のコードを貼り付けます。
function getRandomElements(originalArray, numElements) {
const arrayCopy = [...originalArray];
const indexedArray = arrayCopy.map((element, index) => ({ element, index }));
shuffleArray(indexedArray);
const randomElements = indexedArray.slice(0, numElements)
.sort((a, b) => a.index - b.index)
.map(item => item.element);
return randomElements;
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
const prefix = "name";
const keys = Object.keys(f);
const filteredKeys = keys.filter(key => key.startsWith(prefix));
const items = filteredKeys.map(key => f[key]);
f.result = getRandomElements(items, 2);
⑥ティラノスクリプトコンポーネントの配置
![](https://assets.st-note.com/img/1707109828035-sGNnKWS3Fc.png?width=1200)
ティラノスクリプトコンポーネントを配置します。
![](https://assets.st-note.com/img/1707109892849-rMDGvTkuX7.png?width=1200)
下記のコードを貼り付けます。
[emb exp="f.result"][p]
⑦ジャンプコンポーネントの配置
![](https://assets.st-note.com/img/1707110234199-AhIY4yLgIT.png?width=1200)
ジャンプコンポーネントを配置します。
ターゲットでbuttonを指定します。
これで、10個の変数の値から、2つの値をランダムに取り出す手順は完了です。
以上で解説を終わります。おつかれさまでした。