random

Random(staticSeed).Next() alternative that will never change implementation and is guaranteed consistent through versions [closed]

时光总嘲笑我的痴心妄想 提交于 2020-01-03 19:30:51
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . I'm looking for something similar like: new Random(staticSeed).Next() but where I'm assured that the implementation will always be consistent through different .NET framework versions. I'm specifically looking for something that: ...produces a sequence of numbers that meet

Type Casting Math.random?

眉间皱痕 提交于 2020-01-03 17:41:13
问题 Had a look around the questions on this site and could not quite find the answer I was looking for about type casting the Math.random() method from double to int. My question is, why does Math.random only return a 0 without parentheses whereas it returns random numbers when it is contained within the parentheses? The first part of code returns 0: int number; number = (int) Math.random() * 10; System.out.println("\nThe random number is " + number); This code works however: int number; number =

Generating (very) large non-repeating integer sequence without pre-shuffling

依然范特西╮ 提交于 2020-01-03 17:34:17
问题 Background I have a simple media client/server I've written, and I want to generate a non-obvious time value I send with each command from the client to the server. The timestamps will have a fair bit of data in them (nano-second resolution, even if it's not truly accurate, due to limitations of timer sampling in modern operating systems), etc. What I'm trying to do (on Linux, in C), is to generate a one-to-one sequence of n-bit values (let's assume data is store in 128bit array-of-int

Efficiently select random rows from large resultset with LINQ (ala TABLESAMPLE)

泪湿孤枕 提交于 2020-01-03 17:28:33
问题 I want to select a handful of random rows from the results of a complex query on a very large table (many millions of rows). I am using SQL Server 2008, and the proper way to do this efficiently seems to be the TABLESAMPLE clause. Note 1: I am not interested in the popular "order by NEWID()" solution - it is inefficient for large tables. Note 2: Since my query is complex, I do not want to have to first calculate the COUNT over it, if possible. Note 3: Since the resultset is huge, I do not

Efficiently select random rows from large resultset with LINQ (ala TABLESAMPLE)

岁酱吖の 提交于 2020-01-03 17:28:06
问题 I want to select a handful of random rows from the results of a complex query on a very large table (many millions of rows). I am using SQL Server 2008, and the proper way to do this efficiently seems to be the TABLESAMPLE clause. Note 1: I am not interested in the popular "order by NEWID()" solution - it is inefficient for large tables. Note 2: Since my query is complex, I do not want to have to first calculate the COUNT over it, if possible. Note 3: Since the resultset is huge, I do not

Octave - random generate number

十年热恋 提交于 2020-01-03 17:21:09
问题 I have a simple question about randomly generating numbers in Octave/Matlab. How do I randomly generate a (one!) number (that is either 0 or 1)? I could really use an example. Thanx 回答1: Use rand, which generates a uniform pseudo-random number in the range 0..1, and then test this value against a suitable threshold, e.g. 0.5 for equal probability of 1 or 0: r = rand > 0.5 回答2: You should use randi for integer random numbers: randi(2) - 1 回答3: For the sake of variety, here's another way floor

equivalent vb code for a java code

我是研究僧i 提交于 2020-01-03 17:20:34
问题 Can anyone tell me what exactly does this Java code do? SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] bytes = new byte[20]; synchronized (random) { random.nextBytes(bytes); } return Base64.encode(bytes); Step by step explanation will be useful so that I can recreate this code in VB. Thanks 回答1: Using code snippets you can get to something like this Dim randomNumGen As RandomNumberGenerator = RNGCryptoServiceProvider.Create() Dim randomBytes(20) As Byte randomNumGen

Random Number, Math.floor(…) vs Math.ceil(…)

假如想象 提交于 2020-01-03 17:20:30
问题 I've seen a lot of code where random numbers are generated like // random integers in the interval [1, 10] Math.floor(Math.random()*10 + 1) Anyway, I feel like I'm missing something. Why don't people use the more succint way Math.ceil(Math.random()*10); ? I tried to test the randomness and it seems true so far. In fact, the subsequent code // will generate random integers from 1 to 4 var frequencies = [ 0, 0, 0, 0, 0 ]; // not using the first place var randomNumber; for ( var i = 0; i < 1

Is map iteration sufficiently random for randomly selecting keys?

我只是一个虾纸丫 提交于 2020-01-03 17:02:20
问题 Can I rely on the random iteration order of maps to implement a random "pairing" of clients in a web application? I've tried looking around but can't seem to find a breakdown of how random this randomness is. The algorithm would look something like: var clients map[Client]struct{} func PairClient(c Client) (Client, error) { for m := range clients { if m != c { return m, nil } } return nil, fmt.Errorf("lobby: insufficient number of clients") } Would this be sufficient when there are >1000

Is map iteration sufficiently random for randomly selecting keys?

佐手、 提交于 2020-01-03 17:02:13
问题 Can I rely on the random iteration order of maps to implement a random "pairing" of clients in a web application? I've tried looking around but can't seem to find a breakdown of how random this randomness is. The algorithm would look something like: var clients map[Client]struct{} func PairClient(c Client) (Client, error) { for m := range clients { if m != c { return m, nil } } return nil, fmt.Errorf("lobby: insufficient number of clients") } Would this be sufficient when there are >1000