How to use the @mathigon/fermat.Random.integer function in @mathigon/fermat

To help you get started, we’ve selected a few @mathigon/fermat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mathigon / textbooks / content / divisibility / functions.ts View on Github external
let n2 = Random.smart(9, 'divisibility-game-number') + 2;

    let x, y;
    switch (answer) {
      case 0:
        x = n1;
        y = n1 * n2;
        break;  // factor
      case 1:
        x = n1 * n2;
        y = n1;
        break;  // multiple
      case 2:
        x = n1;
        y = (n1 * n2) + [1, -1][Random.integer(1)];  // neither
        if (Random.integer(1)) [x, y] = [y, x];
    }

    $el.bindObservable(observable({x, y}));

    let $buttons = $el.$$('.factor-bubble');
    $buttons[0].on('click', answer === 0 ? success : error);
    $buttons[1].on('click', answer === 1 ? success : error);
    $buttons[2].on('click', answer > 1 ? success : error);

    $el.on('success', function () {
      for (const [i, $b] of $buttons.entries()) {
        if (answer === i) {
          $b.addClass('success');
          $b.css('transform', 'none');
        } else {
          $b.children[0].exit('pop');
github mathigon / textbooks / content / divisibility / functions.ts View on Github external
let n1 = Random.smart(9, 'divisibility-game-number') + 2;
    let n2 = Random.smart(9, 'divisibility-game-number') + 2;

    let x, y;
    switch (answer) {
      case 0:
        x = n1;
        y = n1 * n2;
        break;  // factor
      case 1:
        x = n1 * n2;
        y = n1;
        break;  // multiple
      case 2:
        x = n1;
        y = (n1 * n2) + [1, -1][Random.integer(1)];  // neither
        if (Random.integer(1)) [x, y] = [y, x];
    }

    $el.bindObservable(observable({x, y}));

    let $buttons = $el.$$('.factor-bubble');
    $buttons[0].on('click', answer === 0 ? success : error);
    $buttons[1].on('click', answer === 1 ? success : error);
    $buttons[2].on('click', answer > 1 ? success : error);

    $el.on('success', function () {
      for (const [i, $b] of $buttons.entries()) {
        if (answer === i) {
          $b.addClass('success');
          $b.css('transform', 'none');
        } else {
github mathigon / textbooks / content / probability / functions.ts View on Github external
const sureBtn = new OneTimeButton([$step.$('.sure')!], () => {
    if (!attempt) {
      // The first time, we cheat and force the "more likely" event
      [car, opened] = Random.shuffle([0, 1, 2].filter(i => i !== selected));
    } else {
      car = Random.integer(3);
      opened = Random.shuffle([0, 1, 2].filter(i => i !== car && i !== selected))[0];
    }

    decided = true;
    $monty.removeClass('selectable');
    $doors[car].addClass('car');

    setTimeout(() => {
      $doors[opened!].addClass('open');
      $reveals[2].addClass('visible');
      $reveals[3].addClass('visible');
    }, 1000);
  });
github mathigon / textbooks / content / circles / functions.ts View on Github external
const num = digits.map((n, i) => {
      if (i > p * length + 1) return '';
      if (isOneOf(n, ',', '.')) return n;
      if (i > p * length - 1) return Random.integer(10);
      return n;
    });
    $el.text = num.join('');
github mathigon / textbooks / content / graph-theory / functions.ts View on Github external
const points = shuffle(n);
    const edges: number[][] = [];

    function addEdge(u: number, v: number) {
      if (u === v) return;
      let edge = new Segment(points[u], points[v]);
      for (let i = 0; i < edges.length; ++i) {
        const e2 = new Segment(points[edges[i][0]], points[edges[i][1]]);
        if (edge.equals(e2) || Segment.intersect(edge, e2)) return;
      }

      edges.push([u, v]);
    }

    for (let i = 0; i < n; ++i) addEdge(i, Random.integer(n));
    for (let i = 0; i < n; ++i) for (let j = i + 1; j < n; ++j) addEdge(i, j);

    graph.load(n, edges, shuffle(n));

    while (!intersect(graph.edges) && n > 4) {
      graph.arrange(shuffle(n));
    }
    creating = false;
  }