相关疑难解决方法(0)

随机化List <T>

在C#中随机化通用列表顺序的最佳方法是什么?我在一个列表中有一组有限的75个数字,我想为其分配一个随机顺序,以便为抽奖类型的应用程序绘制它们.

c# generic-list

795
推荐指数
13
解决办法
40万
查看次数

什么是随机(Java 7)中的181783497276652981和8682522807148012?

为什么1817834972766529818682522807148012在选择Random.java

这是Java SE JDK 1.7的相关源代码:

/**
 * Creates a new random number generator. This constructor sets
 * the seed of the random number generator to a value very likely
 * to be distinct from any other invocation of this constructor.
 */
public Random() {
    this(seedUniquifier() ^ System.nanoTime());
}

private static long seedUniquifier() {
    // L'Ecuyer, "Tables of Linear Congruential Generators of
    // Different Sizes and Good Lattice Structure", 1999
    for (;;) {
        long current …
Run Code Online (Sandbox Code Playgroud)

java random

110
推荐指数
2
解决办法
4032
查看次数

为什么要使用C#类System.Random而不是System.Security.Cryptography.RandomNumberGenerator?

为什么有人会使用System.Random中的"标准"随机数生成器,而不是总是使用System.Security.Cryptography.RandomNumberGenerator(或其子类,因为RandomNumberGenerator是抽象的)的加密安全随机数生成器?

Nate Lawson 在13:11分钟的Google Tech Talk演讲中告诉我们" Crypto Strikes Back ",不要使用Python,Java和C#中的"标准"随机数生成器,而是使用加密安全版本.

我知道两个版本的随机数生成器之间的区别(参见问题101337).

但是,有什么理由不总是使用安全随机数发生器?为什么要使用System.Random?性能或许?

.net c# random cryptography

77
推荐指数
8
解决办法
4万
查看次数

RNGCryptoServiceProvider的优缺点

什么是使用的利弊System.Security.Cryptography.RNGCryptoServiceProviderVS System.Random.我知道这RNGCryptoServiceProvider是"更随机",即黑客可预测性更低.任何其他利弊?


更新:

根据回复,以下是目前使用的利弊RNGCryptoServiceProvider:

优点

  • RNGCryptoServiceProvider 是一个更强大的加密随机数,这意味着它更适合确定加密密钥等.

缺点

  • Random更快,因为它是一个更简单的计算; 当在模拟或长时间计算中使用加密随机性不重要时,应该使用它.注意:有关模拟的详细信息,请参阅Kevin的答案 - Random不一定是随机的,您可能希望使用不同的非加密PRNG.

.net c# random

68
推荐指数
4
解决办法
2万
查看次数

远程随机数,是这样的吗?

有人可以验证这种方法.我需要在两个长度范围内的长型号码.我使用返回int的.NET Random.Next(min,max)函数.如果我将long除以2,生成随机数并最终再乘以2,我的推理是否正确?或者我太热情......我明白我的随机解决方案会减少但是还有其他错误导致没有这样的随机数.

long min = st.MinimumTime.Ticks;    //long is Signed 64-bit integer
long max = st.MaximumTime.Ticks;
int minInt = (int) (min / 2);      //int is Signed 64-bit integer
int maxInt = (int) (max / 2);      //int is Signed 64-bit integer

Random random = new Random();
int randomInt = random.Next(minInt, maxInt);
long randomLong = (randomInt * 2);
Run Code Online (Sandbox Code Playgroud)

random c#-4.0

45
推荐指数
5
解决办法
5万
查看次数

如何生成.NET 4 GUID?

我知道这里有很多问题以及雷蒙德的优秀(通常)帖子.但是,由于创建GUID的算法明显改变,我发现很难掌握任何最新信息.在MSDN似乎试图提供尽可能少的信息成为可能.

有关如何在.NET 4中生成GUID的已知信息?改变了什么,它如何影响安全性("随机性")和完整性("唯一性")?

我感兴趣的一个特定方面:在v1中,由于涉及时间戳和计数器,似乎几乎不可能在单个机器上再次生成相同的GUID.在v4中,情况不再如此(我被告知),因此在一台机器上获得相同GUID的机会增加了

.net guid .net-4.0

43
推荐指数
2
解决办法
2万
查看次数

在多线程应用程序中使用Random的正确方法

好.以下是我所知道的不起作用:

int Rand()
{
    //will return the same number over and over again
    return new Random().Next();
}

static Random rnd=new Random();

int Rand()
{
    //if used like this from multiple threads, rnd will dissintegrate 
    //over time and always return 0
    return rnd.Next();
}
Run Code Online (Sandbox Code Playgroud)

这将正常工作,但如果由多个线程使用,CPU使用率上升,我不想要,我认为没有必要:

int Rand()
{
    lock(rnd)
    {
        return rnd.Next();
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,c#是否有一个线程安全的Random类,或者更好的方法来使用它?

c# random

43
推荐指数
3
解决办法
1万
查看次数

如何使用Linq获取随机对象

我想在linq中获取一个随机对象.我就是这样做的.

//get all the answers
var Answers = q.Skip(1).Take(int.MaxValue);
//get the random number by the number of answers
int intRandomAnswer = r.Next(1, Answers.Count());
int count = 0;

//locate the answer
foreach(var Answer in Answers)
{
    if (count == intRandomAnswer)
    {
        SelectedPost = Answer;
        break;
    }
    count++;
}
Run Code Online (Sandbox Code Playgroud)

这是最好的方法吗?

.net c# linq random

28
推荐指数
3
解决办法
3万
查看次数

随机生成数字1超过90%并行

考虑以下程序:

public class Program
{
     private static Random _rnd = new Random();
     private static readonly int ITERATIONS = 5000000;
     private static readonly int RANDOM_MAX = 101;

     public static void Main(string[] args)
     {
          ConcurrentDictionary<int,int> dic = new ConcurrentDictionary<int,int>();

          Parallel.For(0, ITERATIONS, _ => dic.AddOrUpdate(_rnd.Next(1, RANDOM_MAX), 1, (k, v) => v + 1));

          foreach(var kv in dic)
             Console.WriteLine("{0} -> {1:0.00}%", kv.Key, ((double)kv.Value / ITERATIONS) * 100);
     }
}
Run Code Online (Sandbox Code Playgroud)

这将打印以下输出:

(注意每次执行时输出会有所不同)

> 1 -> 97,38%
> 2 -> 0,03%
> 3 -> …
Run Code Online (Sandbox Code Playgroud)

.net c# random parallel-processing

20
推荐指数
2
解决办法
1554
查看次数

Parallel.For和For产生不同的结果

如果我运行此测试:

 var r = new Random();
 var ints = new int[13];
 Parallel.For(0, 2000000, i => {            
     var result = r.Next(1, 7) + r.Next(1, 7);
     ints[result] += 1;
 });
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

2: 92,14445
3: 0,41765
4: 0,62245
5: 0,82525
6: 1,04035
7: 1,25215
8: 1,0531
9: 0,8341
10: 0,6334
11: 0,4192
12: 0,2109
Run Code Online (Sandbox Code Playgroud)

当我使用常规For:

for (int i = 0; i < 2000000; i++) {
    var result = r.Next(1, 7) + r.Next(1, 7);
    ints[result] += 1;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

2: 2,7797
3: 5,58645
4: …
Run Code Online (Sandbox Code Playgroud)

c# parallel-processing multithreading c#-4.0

13
推荐指数
2
解决办法
627
查看次数