random

Generate a random number with no repeating digits?

拥有回忆 提交于 2020-01-04 10:11:09
问题 I'd like to generate a random number with each digit being in range from 0-9 and not repeating itself. Assume finite length of 4. 1234 qualifies, each composite digit is unique. 1123 does not, 1 is repeated How can this be done please? 回答1: To generate the digits: std::vector<int> vec = {0,1,2,3,4,5,6,7,8,9}; // or initialize from array if not c++11 std::random_shuffle(vec.begin(), vec.end()); vec.resize(4); And to join the digits into a single number: int number = 0; for (auto i = vec.begin(

Generating smoothed randoms that follow a distribution

喜欢而已 提交于 2020-01-04 09:48:32
问题 I have two variables, lets call them x and y , which when plotted are the scattered blue points in the graph. I have fitted them using curve_fit from Scipy . I want to generate (lets say 500000) "smoothed" random numbers replicating the distribution followed by x and y . By "smoothed" I mean, I don't want randoms that exactly replicate my data ( x and y ) like in the figure below , with the red diamonds being my data distribution and the histogram being my generated randoms. ( even the

Seedable random number generator, actionscript

*爱你&永不变心* 提交于 2020-01-04 08:27:33
问题 I am looking for a random number generator in actionscript 3.0 that i can seed. The Math.random() does not have this functionality. Thanks. 回答1: Here you go http://gskinner.com/blog/archives/2008/01/source_code_see.html 回答2: I mostly use this seeded random function, which is very fast. var seed:int = 777; const MAX_RATIO:Number = 1 / int.MAX_VALUE; const MIN_MAX_RATIO:Number = -MAX_RATIO; function random():Number { seed ^= (seed << 21); seed ^= (seed >>> 35); seed ^= (seed << 4); if (seed > 0

Why does my C random number generator only return “42”?

☆樱花仙子☆ 提交于 2020-01-04 08:20:14
问题 As awesome an accidental feature as this is, it makes for a lousy way to "shuffle" an array of "cards". The fact that I'm getting the same number tells me I've having some problem in picking separate seeds each time. Am I using srand48 or the time(NULL) call improperly? Is there some underlying logic flaw I'm missing? Is there just not enough time inbetween iterations for the value of time() to be different? The code is being run on Linux. void shuffle() { int i_rnd; /* Integer random number,

Generating random words

旧巷老猫 提交于 2020-01-04 07:20:00
问题 I'm trying to create a string that has a set amount of different words I include in a list, however the code I use only uses one word at random, not a different word for every word printed. This is my code: import random words = ['hello', 'apple', 'something', 'yeah', 'nope', 'lalala'] print random.choice(words) * 5 An example output would be: hellohellohellohellohello An example expected output would be: appleyeahhellonopesomething Can anyone tell me what I'm doing wrong? 回答1: random.choice

java use Regular Expressions to generate a string [duplicate]

十年热恋 提交于 2020-01-04 07:01:25
问题 This question already has answers here : Using Regex to generate Strings rather than match them (12 answers) Closed last year . i want to generate random String who have this form [A-Za-z0-9]{5,10} I don't have any idea how to do it, i should use regular expressions or random function ? 回答1: I'd stick to a Java-solution in this case, something along the lines of: private String allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGRHIJKLMNOPQRSTUVWXYZ0123456789"; public String getRandomValue(int

java use Regular Expressions to generate a string [duplicate]

China☆狼群 提交于 2020-01-04 07:01:24
问题 This question already has answers here : Using Regex to generate Strings rather than match them (12 answers) Closed last year . i want to generate random String who have this form [A-Za-z0-9]{5,10} I don't have any idea how to do it, i should use regular expressions or random function ? 回答1: I'd stick to a Java-solution in this case, something along the lines of: private String allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGRHIJKLMNOPQRSTUVWXYZ0123456789"; public String getRandomValue(int

Sampling unique column indexes for each row of a numpy array

∥☆過路亽.° 提交于 2020-01-04 05:43:15
问题 I want to generate a fixed number of random column indexes (without replacement) for each row of a numpy array. A = np.array([[3, 5, 2, 3, 3], [1, 3, 3, 4, 5], [3, 5, 4, 2, 1], [1, 2, 3, 5, 3]]) If I fixed the required column number to 2, I want something like np.array([[1,3], [0,4], [1,4], [2,3]]) I am looking for a non-loop Numpy based solution. I tried with choice, but with the replacement=False I get error ValueError: Cannot take a larger sample than population when 'replace=False' 回答1:

Random number In C# [duplicate]

邮差的信 提交于 2020-01-04 04:18:07
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Random number generator not working the way I had planned (C#) I created a method that returns me a random number: public static int SelectRandomMachine(int max) { int seed = (int)DateTime.Now.Ticks; Random rndNumber = new Random(seed); int randMachine = rndNumber.Next(0, max); return randMachine; } if I call the method two times, currently it's return me the same random number: randM1 = SelectRandomMachine

rand() seeding with time() problem

吃可爱长大的小学妹 提交于 2020-01-04 04:06:07
问题 I'm having difficulty figuring out how to use rand() and seeding it with time() using Xcode. I want to generate random decimal numbers between 0 and 1. The code gives me seemingly random numbers for elements 1 and 2, but element 0 is always somewhere around 0.077. Any ideas why this would be? My code is: #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { double array[3]; double rand_max = RAND_MAX; srand((int)time(NULL)); for (int iii = 0; iii < 3; iii++) array[iii] =