• 请不要在回答技术问题时复制粘贴 AI 生成的内容
pyengwoei
V2EX  ›  程序员

这段 JS 转 C# Math.floor(65536 * (1 + Math.random())).toString(16).substring(1)

  •  
  •   pyengwoei · Oct 9, 2016 · 2887 views
    This topic created in 3699 days ago, the information mentioned may be changed or developed.
    帮忙转成 C# 的写法一下, TY
    8 replies  •  2016-10-09 14:45:31 +08:00
    lrh3321
    lrh3321
    Oct 9, 2016❤️1
    1
    var rand = new Random();
    ((int)(65536 * (1.0d+ rand.NextDouble()))).ToString("x");

    大概是这样吧
    pyengwoei
    pyengwoei
    OP
    Oct 9, 2016
    2
    @lrh3321 我试下
    dong3580
    dong3580
    Oct 9, 2016❤️1
    3
    Random rd = new Random();
    Convert.ToInt32(Math.Floor(65536 * (1 + rd.NextDouble()))).ToString("x");
    pyengwoei
    pyengwoei
    OP
    Oct 9, 2016
    4
    @lrh3321 得到的是 1f936 五位数的,比 JS 里面多了一位
    pyengwoei
    pyengwoei
    OP
    Oct 9, 2016
    5
    @dong3580 我得到的也是 5 位的字符,也比 JS 多了一位,是 toString(16) 这里这个没有加的原因吗
    Arthur2e5
    Arthur2e5
    Oct 9, 2016❤️1
    6
    少了一步 substring ,把第一个字切掉就是……
    gucheen
    gucheen
    Oct 9, 2016❤️1
    7
    Random rd = new Random();
    Convert.ToInt32(Math.Floor(65536 * (1 + rd.NextDouble()))).ToString("x").Substring(1);

    加上就是了。。。
    pyengwoei
    pyengwoei
    OP
    Oct 9, 2016
    8
    @gucheen 感谢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1791 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:59 · PVG 23:59 · LAX 07:59 · JFK 10:59
    ♥ Do have faith in what you're doing.