cubing/scramble

cubing/scramble is the part of cubing.js that lets you generate fair random-state scrambles.

Random Scramble

Use randomScrambleForEvent to get a scramble. This returns a JavaScript Promise for an Alg, which is easiest to use with await.

<script type="module">
import { randomScrambleForEvent } from "https://cdn.cubing.net/v0/js/cubing/scramble";

const scramble = await randomScrambleForEvent("333"); console.log(scramble.toString());
</script>
"F' R' F' U2 B D' L' U F R B' R2 F' L2 F2 L2 D2 B R2 L2 F'"

You can generate scrambles for all official WCA events:

<script type="module">
import { randomScrambleForEvent } from "https://cdn.cubing.net/v0/js/cubing/scramble";

(await randomScrambleForEvent("333")).log();
(await randomScrambleForEvent("333bf")).log();
(await randomScrambleForEvent("333fm")).log();
(await randomScrambleForEvent("222")).log();
(await randomScrambleForEvent("444")).log();
(await randomScrambleForEvent("777")).log();
(await randomScrambleForEvent("sq1")).log();
(await randomScrambleForEvent("minx")).log();
(await randomScrambleForEvent("clock")).log();
</script>
Alg {} "R' F2 … L U'" Alg {} "R' B2 … R Fw' Dw" Alg {} "R' U' F D … L' R' U' F" Alg {} "Fw2 Lw2 L …" Alg {} "(-2, 6) / …" Alg {} "R-- D++ R++ D-- …" Alg {} "UR1- DR1+ …"

In addition, several unofficial events are supported:

<script type="module">
import { randomScrambleForEvent } from "https://cdn.cubing.net/v0/js/cubing/scramble";

(await randomScrambleForEvent("fto")).log();
(await randomScrambleForEvent("master_tetraminx")).log();
</script>
Alg {} "U L' BL' B' U' …" Alg {} "L' b B' r' …"

Note that:

Using a file server

If you want to generate scrambles using cubing.js, please ensure the page is accessed from a server using a URL that starts with one of the following:

Scramble generation will not work in Google Chrome if your browser is showing a file from your file system using a URL that starts with:

If you need to use a file server for local development, you may find one of the following commands useful:

Prefetching and debug options

<script type="module">
// Note that this is `cubing/search`, not `cubing/scramble`.
import { setSearchDebug } from "https://cdn.cubing.net/v0/js/cubing/search";

// You can specify any subset of debug options.
setSearchDebug({
  logPerf: false, // Disable console info like scramble generation durations.
  scramblePrefetchLevel: "none", // Never prefetch scrambles.
});
</script>

By default, cubing.js will prefetch a new scramble under these conditions:

cubing.js will then start generating a scramble for the same event as the previous request, which will be available more quickly (often almost instantly) to answer the next scramble request for that event. This is particularly useful for timer apps, where the next scramble can be prepared while the cuber is solving.

Scramble generation always happens in a worker, so this will not affect the performance of your app. However, it can waste a little bit of time/work if you request a scramble for a new event while the one for the previous event is still being prefetched. This is unlikely to be a serious concern for apps, but you can turn off this functionality setting scramblePrefetchLevel like in the code above.

With <twisty-player>

You can assign a scramble or a scramble Promise to <twisty-player>. Here is a simple way to display a scramble alg with an associated player:

<twisty-alg-viewer for="main-player"></twisty-alg-viewer>
<twisty-player id="main-player"></twisty-player>

<script type="module">
  import "https://cdn.cubing.net/v0/js/cubing/twisty";
  import { randomScrambleForEvent } from "https://cdn.cubing.net/v0/js/cubing/scramble";
  
  const scramblePromise = randomScrambleForEvent("333");
  document.querySelector("#main-player").alg = scramblePromise;
</script>

Try it.