Tag:

从范围中选择但排除某些数字

是否可以从给定范围(1-90)中选择一个随机数,但不包括某些数字。 排除的数字是动态创建的,但可以说它们分别为3,8和80。 我已设法创建随机数生成器,但无法识别任何让我满足我的要求的function。 Random r = new Random(); this.num = r.Next(1, 90); 要排除的数字是先前生成的数字。 因此,如果随机数为1,则会将其添加到排除的数字列表中。

随机数发生器 – 可变长度

目前我的程序(见下文)生成长度为8-12个字符的随机字符串(由数字组成)。 public static string GenerateNewCode(int CodeLength) { string newCode = String.Empty; int seed = unchecked(DateTime.Now.Ticks.GetHashCode()); Random random = new Random(seed); // keep going until we find a unique code ` do { newCode = random.Next(Convert.ToInt32(Math.Pow(10, CodeLength – 1)), Convert.ToInt32(Math.Pow(10, CodeLength) – 1)).ToString(“0000”); } while (!ConsumerCode.isUnique(newCode)); // return return newCode; } 但是,它们是该方法的问题,当codeLength为10或更大时,它会导致错误,因为10 9大于int32.MaxValue 。 不知道如何解决这个问题。

如何生成唯一的订单号?

我正在寻找一种生成唯一订单ID的好方法。 你能看到下面的代码有什么问题吗? int customerId = 10000000; long ticks = DateTime.UtcNow.Ticks; long orderId = customerId + ticks; int orderNumber = orderId.GetHashCode(); 在创建订单之前,我将检查数据库中的数字是否唯一。

如何随机化数组中的数字

我有一个像这样的数组: int[] numbers = new [] { 1, 2, 3, 4 }; 我想随机化这个(每次都不同),这样它就会使另一个数组具有相同的大小和数字,但每次都以不同的顺序排列。

C#随机数不是“随机的”

我知道C#Random类不会产生“真正的随机”数字,但我想出了这个代码的问题: public void autoAttack(enemy theEnemy) { //Gets the random number float damage = randomNumber((int)(strength * 1.5), (int)(strength * 2.5)); //Reduces the damage by the enemy’s armor damage *= (100 / (100 + theEnemy.armor)); //Tells the user how much damage they did Console.WriteLine(“You attack the enemy for {0} damage”, (int)damage); //Deals the actual damage theEnemy.health -= […]