dsb0003795 2009-08-22 13:53
浏览 82
已采纳

PHP相当于C#中的uniqid()

trying to duplicate some PHP code in C#. The PHP code uses the uniqid function with the more entropy parameter set to true. Any idea of how to best duplicate this in C#?

http://us2.php.net/uniqid

  • 写回答

2条回答 默认 最新

  • duanrenchuo9244 2009-08-22 13:56
    关注

    something like:

    string uniqid(string prefix, bool more_entropy) {
        if(string.IsNullOrEmpty(prefix)) 
          prefix = string.Empty;
    
        if (!more_entropy) {
          return (prefix + System.Guid.NewGuid().ToString()).Left(13);
        } else {
          return (prefix + System.Guid.NewGuid().ToString() + System.Guid.NewGuid().ToString()).Left(23);
        }
     }
    

    Edit: To reflect comments

    This is a very simple off the top of my head solution, and I'd suggest that for a more rigorous solution you'd investigate proper 'random' number' generation such as via System.Math namespace directly. Please see these other SO questions and answers on random number generation.

    System.Guid.NewGuid().ToString()

    Will give you a unique string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?