周易筮算 ☯️

本页算法模拟“大衍筮法”得到卦象,在客户端直接进行计算。用你自己设备的 CPU 和内存代替筮草,算上一挂吧!

大衍之数五十,其用四十有九。分而为二以象两,挂一以象三,揲之以四以象四时,归奇于扐以象闰,五岁再闰,故再扐而后挂。

☯️ 卦象

断挂功能建设中,敬请期待

☯️ 附:大衍筮法 模拟算法

// get random integer between min and max, including both
const getRandomIntBetween = (min, max) => {
  if (!(min < max)) {
    throw new Error('min must be smaller than max');
  }
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
};

// 大衍筮法 算出单个爻 算法
const getYao = () => {
  let leftPoints = 0;
  let rightPoints = 0;
  //大衍之数五十,其用四十有九
  let remainedPoints = 50 -1;
  for (let i = 0; i < 3; i++) {
    // 分而为二以象两
    leftPoints = getRandomIntBetween(1, remainedPoints - 1);
    rightPoints = remainedPoints - leftPoints;
    // 挂一以象三
    rightPoints -= 1;
    // 揲之以四以象四时,归奇于扐以象闰
    const leftRemainder = (leftPoints % 4) || 4;
    const rightRemainder = (rightPoints % 4) || 4;
    // 五岁再闰,故再扐而后挂
    remainedPoints = remainedPoints - leftRemainder - rightRemainder - 1;
  }
  return (remainedPoints) / 4;
};