random

Java: create array with random int's (int's can only be used once)

倾然丶 夕夏残阳落幕 提交于 2020-01-01 19:47:11
问题 I have an array called arr , with place for 15 elements. I need to place the numbers 1 through 15 in a random order into that array. Here is what I have tried: int[] arr = new int[15]; int i,j,k,n; for (i = 0; i<15; i++) { for (j=0; j<15; j++) { n = (int)(Math.random() * 14 + 1); if (rij[j] != n) { rij[i] = n; break; } } } Thanks! :) 回答1: Use an ArrayList and fill it up with numbers 1 to 15. Shuffle the list. Convert it to an array. 回答2: This seems like homework (or an interview question?).

How to generate a random number in the constructor of a class in C#

ε祈祈猫儿з 提交于 2020-01-01 19:26:29
问题 I know that it is not a good practice to call a method in the constructor of a class in C# but I stuck on something strange. My problem is that when I create an object of my class I need to assign a field in my object with a random number. for instance class RandomNumberHandler { private int randomNumber; public RandomNumberHandler() { this.randomNumber = GenerateRandomNumber(); } private int GenerateRandomNumber() { return (new Random()).Next(3000) + 1000; } } In my case I need a four digit

How to get make stats in constant memory

孤者浪人 提交于 2020-01-01 18:19:54
问题 I have a function, which creates some random numerical results. I know, that the result will be an integer in a (small, a - b approx 50) range a, b . I want to create a function which execute the above function let's say 1000000 times and calculates, how often the each result appears. (The function takes a random generator to produce the result.) The problem is, I don't know how to do this in constant memory without hard-coding the range's length. My (bad) approach is like this: values ::

Hash of unique value = unique hash?

回眸只為那壹抹淺笑 提交于 2020-01-01 17:12:24
问题 Theoretically does hashing a unique value yield a unique value? Let's say I have a DB table with 2 columns: id and code. id is an auto-incrementing int and code is a varchar. If I do ... $code = sha1($id); ... and then store $code into the same row as $id. Will my code column be unique as well? What about if I append the current time? eg: $code = sha1($id . time()); Thanks. 回答1: In general, the answer is no. This is trivial to show: SHA-1 has 2^160 different outputs - 160 bits, but there are

Is there an elegant and efficient way to implement weighted random choices in golang? Details on current implementation and issues inside

扶醉桌前 提交于 2020-01-01 17:08:14
问题 tl;dr: I'm looking for methods to implement a weighted random choice based on the relative magnitude of values (or functions of values) in an array in golang. Are there standard algorithms or recommendable packages for this? Is so how do they scale? Goals I'm trying to write 2D and 3D markov process programs in golang. A simple 2D example of such is the following: Imagine one has a lattice, and on each site labeled by index (i,j) there are n(i,j) particles. At each time step, the program

iOS Gaussian distribution of random numbers [duplicate]

大城市里の小女人 提交于 2020-01-01 11:56:27
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Generating a random Gaussian double in Objective-C/C Is there any way of getting a random number not from a uniform distribution, but from a Gaussian (Normal, Bell Curve) distribution in iOS? All the random number generators I have found are basically uniform and I want to make the numbers cluster around a certain point. Thanks! 回答1: Just use a uniform distribution generator and apply the Box-Muller Transform:

How to generate a sequence of n random positive integers which add up to some value?

偶尔善良 提交于 2020-01-01 11:45:12
问题 I'm trying to generate an array of integers contains randoms adding up to a particular value. Here's my code: private long[] getRandoms(long size , long sum) throws Exception { double iniSum = 0; System.out.println("sum = " + sum); long[] ret = new long[(int) size]; for (int i = 0 ; i < ret.length; i++) { ret[i] = randomInRange(1, sum); iniSum += ret[i]; } double finSum = 0; for (int i = 0 ; i < ret.length; i++) { ret[i] = Math.round((sum * ret[i]) / iniSum); System.out.println("ret[" + i +"]

Selecting a valid random enum value in a general way

柔情痞子 提交于 2020-01-01 10:45:24
问题 Let's say we have an enumerated type E . enum class E : underlying_type_of_E { v1 = uE1, v2 = uE2, //... vN = uEN }; typedef typename std::underlying_type<E>::type uE; In general, not all values of uE are valid values of E , because we can choose the relationship between them. Is there a general way of creating random, valid (named in definition, not assignable ), values of E? This, for example would not work: std::mt19937 m; std::uniform_int_distribution<uE> randomUE(0, std::numeric_limits

Algorithm to generate random order of elements

点点圈 提交于 2020-01-01 08:59:29
问题 How to randomize order of approximately 20 elements with lowest complexity? (generating random permutations) 回答1: Knuth's shuffle algorithm is a good choice. 回答2: Some months ago I blogged about obtaining a random permutation of a list of integers. You could use that as a permutation of indexes of the set containing your elements, and then you have what you want. Generating random int list in F# Uniformly distributed random list permutation in F# In the first post I explore some possibilities

Random numbers keep coming out the same, despite random seed being used

坚强是说给别人听的谎言 提交于 2020-01-01 08:56:04
问题 I have the following small piece of code: REAL(8) :: x INTEGER :: i call system_clock(i) WRITE(*,*) 'cpu time', i CALL random_seed(i) CALL random_number(x) WRITE(*,*) 'uniform RandVar', x CPU time is working fine, but every time I run this I get the same uniform RandVar number = 0.99755959009261719 , almost like random_number is using the same default seed over and over again and ignoring random seed. What am I doing wrong? 回答1: The same seed may well be being used: that is processor