V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
pyengwoei
V2EX  ›  程序员

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

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

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

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