random

Trouble with C# Random class

孤街醉人 提交于 2020-01-14 11:58:58
问题 I've got a class that represents a coin, which can be flipped with the Coin.Flip() method. Flip() uses random.Next(2); to get either a 0 or a 1 representing heads or tails. This works good.. sort of. For the program, I need to have 2 coins, which I make, lets say coin1 and coin2. coin2 always needs to be flipped straight after coin1, which I can do with: coin1.Flip(); coin2.Flip(); That should work, right? Well it doesn't! Every time I run those two lines of code, both coins end up with the

random.choice broken with dicts

依然范特西╮ 提交于 2020-01-14 11:57:55
问题 There is a restriction on random.choice that the input must be a sequence. This causes non-obvious and hairy behaviour on a dict : >>> d = {0: 'spam', 1: 'eggs', 3: 'potato'} >>> random.choice(d) 'spam' >>> random.choice(d) 'eggs' >>> random.choice(d) 'spam' >>> random.choice(d) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/random.py", line 274, in choice return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty KeyError: 2

Trouble with C# Random class

会有一股神秘感。 提交于 2020-01-14 11:57:24
问题 I've got a class that represents a coin, which can be flipped with the Coin.Flip() method. Flip() uses random.Next(2); to get either a 0 or a 1 representing heads or tails. This works good.. sort of. For the program, I need to have 2 coins, which I make, lets say coin1 and coin2. coin2 always needs to be flipped straight after coin1, which I can do with: coin1.Flip(); coin2.Flip(); That should work, right? Well it doesn't! Every time I run those two lines of code, both coins end up with the

random.choice broken with dicts

南笙酒味 提交于 2020-01-14 11:55:29
问题 There is a restriction on random.choice that the input must be a sequence. This causes non-obvious and hairy behaviour on a dict : >>> d = {0: 'spam', 1: 'eggs', 3: 'potato'} >>> random.choice(d) 'spam' >>> random.choice(d) 'eggs' >>> random.choice(d) 'spam' >>> random.choice(d) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/random.py", line 274, in choice return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty KeyError: 2

Generating unique, random alpha numeric strings

微笑、不失礼 提交于 2020-01-14 11:36:19
问题 I am developing an application which allows users to share a link to a simple survey. For this, I want to generate unique URLs for each survey, so having a URL like: http://myapp.com/aBcDe1F I want the alpha numeric identifier part of the URL to be pseudo random and somewhat short (6-8 characters). Now, generating that is easy, but how do I ensure that they are unique but also pseudo random? Do I have to generate it, then check with a query to the database to ensure it's not been generated

Generating unique, random alpha numeric strings

北战南征 提交于 2020-01-14 11:32:04
问题 I am developing an application which allows users to share a link to a simple survey. For this, I want to generate unique URLs for each survey, so having a URL like: http://myapp.com/aBcDe1F I want the alpha numeric identifier part of the URL to be pseudo random and somewhat short (6-8 characters). Now, generating that is easy, but how do I ensure that they are unique but also pseudo random? Do I have to generate it, then check with a query to the database to ensure it's not been generated

Picking random number between two points in C

走远了吗. 提交于 2020-01-14 10:34:06
问题 I was wondering, is it possible to generate a random number between two limits in c. I.e. my program is set like this: function x { generate random number; } while(1) { function x; delay } so bascially I want a random number to be generated everytime the function is called , but the number has to be between, for example, 100 and 800 I know there is a already made function called random and randmize in stdlib.h I just dont know how to create the upper and lower limits Thank you 回答1: First, don

Probability of already existing file System.IO.Path.GetRandomFileName()

僤鯓⒐⒋嵵緔 提交于 2020-01-14 10:32:22
问题 Recently I got the exception: Message: System.IO.IOException: The file 'C:\Windows\TEMP\635568456627146499.xlsx' already exists. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) This was the result of the following code I used for generating file names: Path.Combine(Path.GetTempPath(), DateTime.Now.Ticks + ".xlsx"); After realising that it is possible to create two files in one Tick, I changed the code to: Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + "

Average of large number of Dice Rolls in Haskell

懵懂的女人 提交于 2020-01-14 10:15:54
问题 In an attempt to learn Haskell better, I'm trying to write a program that displays the average value of the sum of 2 die, rolled X number of times. This is fairly simple in C, Java, Python... but I'm stuck in Haskell. Here's a naive attempt: import System.Random main = do g <- getStdGen let trials = 10000000 let rolls = take trials (randomRs (2, 12) g :: [Int]) let average = div (sum rolls) trials print average For low number of trials, the program works. But when I run this code with ten

在wpf中利用异步lambda编程,模拟数据库连接,防止界面假死

馋奶兔 提交于 2020-01-14 09:56:24
参考《图解C#》第20章异步编程第五节程序,解决在wpf中连接数据库,界面假死问题。 public partial class MainWindow : Window { private bool isConnected = false; public MainWindow() { InitializeComponent(); startWorkButton.Click += async (sender, e) => { SetGuiValues(false, "正在连接数据库"); await DoSomeWork(); if (isConnected==true) { SetGuiValues(true, "数据库连接成功"); } else { SetGuiValues(true, "数据库连接失败"); } }; } /// <summary> /// 模拟连接数据库 /// </summary> /// <returns></returns> private Task DoSomeWork() { Random random = new Random(); int rndValue = random.Next(0, 10); if (rndValue%3==0) { isConnected = true; } else { isConnected = false; }