V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pyengwoei
V2EX  ›  程序员

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

  •  
  •   pyengwoei · 2016-10-09 14:08:32 +08:00 · 1929 次点击
    这是一个创建于 2769 天前的主题,其中的信息可能已经有所发展或是发生改变。
    帮忙转成 C# 的写法一下, TY
    8 条回复    2016-10-09 14:45:31 +08:00
    lrh3321
        1
    lrh3321  
       2016-10-09 14:14:08 +08:00   ❤️ 1
    var rand = new Random();
    ((int)(65536 * (1.0d+ rand.NextDouble()))).ToString("x");

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

    加上就是了。。。
    pyengwoei
        8
    pyengwoei  
    OP
       2016-10-09 14:45:31 +08:00
    @gucheen 感谢了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5129 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 01:10 · PVG 09:10 · LAX 18:10 · JFK 21:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.