zoukankan      html  css  js  c++  java
  • javascript随机数生成公式

    所以,如果你希望生成1到任意值的随机数,公式就是这样的:

    // max - 期望的最大值
    parseInt(Math.random()*max,10)+1;
    Math.floor(Math.random()*max)+1;
    Math.ceil(Math.random()*max);

    如果你希望生成0到任意值的随机数,公式就是这样的:

    // max - 期望的最大值
    parseInt(Math.random()*(max+1),10);
    Math.floor(Math.random()*(max+1));

    如果你希望生成任意值到任意值的随机数,公式就是这样的:

    // max - 期望的最大值
    // min - 期望的最小值 
    parseInt(Math.random()*(max-min+1)+min,10);
    Math.floor(Math.random()*(max-min+1)+min);

  • 相关阅读:
    知识加油站
    markdown 使用
    今日计划
    继续继续
    我回来了
    2020/5/8
    2020/5/10
    明天一定要学习了
    入驻博客园
    this is a test
  • 原文地址:https://www.cnblogs.com/xienh/p/5213026.html
Copyright © 2011-2022 走看看