random

Is there a way to use a mailto: link that includes a timestamp or some sort of unique code?

ぐ巨炮叔叔 提交于 2021-01-28 19:23:45
问题 I'd like to have a link like so: mailto:foo@bar.com?subject=email&body=TIMESTAMP or MATMSG:TO:example@example.com;SUB:email;Body:TIMESTAMP;; But in the subject line or in the body, I'd like it to include any of these options: A timestamp Sort of message like 'Message number #' which counts the number of times it's been used Any sort of unique random code of gibberish I was hoping it would be possible to somehow import the contents from another website like this or something. Is there any way

randomly sort a list with bias

强颜欢笑 提交于 2021-01-28 19:11:19
问题 I have a list as follows: i = [ {'id': '1', 'P': 0.5}, {'id': '2', 'P': 0.4}, {'id': '3', 'P': 0.8}, ... {'id': 'x', 'P': P(x)} ] When I do the following: i.sort(key=lambda x: x['P'], reverse=True) The list gets sorted based on P where the element with the largest P is in front. but what if I want to make it seem randomized so that even a element with a small P value (with very small probability) can be the first item in the list? Is it possible to implement that using the sort() function or

Create a column of random integers, where the min and max integer for each row come from adjacent columns

本秂侑毒 提交于 2021-01-28 18:34:02
问题 I have a data frame: letter <- c("A", "B", "C") min <- c(1, 2, 3) max <- c(4, 5, 6) df <- data.frame(letter, min, max) I want to add a 4th column to df which generates a random number for each row, where the lower and upper limits from which the random integers are sampled from are the min and max values of that row, respectively. I have an inkling that I should use sample() , but when I do it returns a column of values drawn randomly from the referenced column: df$random <- sample(df$max)

Generate 3 distinct random integers via a uniformly distributed RNG

我是研究僧i 提交于 2021-01-28 18:01:25
问题 Suppose I have a randint(A, B) function which generates a uniformly distributed random integer in the (inclusive) range [A, B] . For example, 1 <= randint(1, 10) <= 10 . How can I efficiently randomly generate 3 distinct integers within a certain range? This should be done with exactly 3 calls to randint(A, B) . I don't care which permutation of the three numbers I get; it can be random or have a defined order. I'm not just trying to find any algorithm to do this, but (within reason) the

How to use optional argument of random.shuffle in Python

谁说我不能喝 提交于 2021-01-28 12:19:51
问题 From the doc of random.shuffle(x[, random]), it says: The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random() Could someone please explain what 0-argument function means and give an example of random.shuffle() with the optional argument random ? I searched but couldn't find any example for that case. Also, what did it mean by "this is the function random() "? Does this refer to the optional argument? 回答1: It means

How to constrain items with multiple randomly selected positions so that the average position for each is within a certain range

╄→尐↘猪︶ㄣ 提交于 2021-01-28 11:01:03
问题 Problem: In total, I have 1-300 positions to fill in, I have 50 items, each item has 6 unique positions to choose (300 total positions). The average position for each of these items needs to be in range 145-155 (the closer to 150 the better) Constraints: For each item, each of its 6 positions must fall within a list of fixed ranges (called runs), but not be within the same run, e.g. The runs are: [(1,36), (37,73), (74,110), (111,148), (149,186), (187,225), (226,262), (263, 300)] . So item 1

How to create a random list with only unique values? [duplicate]

天涯浪子 提交于 2021-01-28 08:34:46
问题 This question already has answers here : Python: Random numbers into a list (8 answers) Closed 3 years ago . I want to create a random list with 100 elements. So far, so good... Example 1: works fine, because the function has a range which is big enough not to repeat any value list = random.sample(range(1,200),100) Example 2: Does not work, because the range is too small (sample is larger than population) list = random.sample(range(50,80),100) I am looking for a way to create that random list

Replacing every number in a string with a random char

ぃ、小莉子 提交于 2021-01-28 07:36:34
问题 I want to replace every number in a string like ABC123EFG with another random char. My idea was to generate a random string with the number of all numbers in $str and replace every digit by $array[count_of_the_digit] , is there any way to do this without a for-loop, for example with regex? $count = preg_match_all('/[0-9]/', $str); $randString = substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz", $count)), 0, $count); $randString = str_split($randString); $str = preg_replace('/[0-9]+/'

C++: Performance for drawing random boolean values

末鹿安然 提交于 2021-01-28 06:40:53
问题 I am drawing random boolean numbers with std::mt19937 rng; std::uniform_int_distribution<int> dis(0, 1); In extreme cases, drawing these numbers can take up to 40% of the CPU time of my process. Is there any way to make this faster? 回答1: I would get rid of uniform_int_distribution . One invocation of std::mt19937::operator() (i.e. doing rng() ) will give you 32 random bits, which you can use one by one. A benchmark shows that this method is about 23 times faster. 回答2: You might be happy with

How to generate 8 bytes unique random number in python?

戏子无情 提交于 2021-01-28 06:06:32
问题 Is there any way to generate a unique random number that has 8 bytes size in python language? I used the UUID library but it has 16 bytes which are not aligned with my requirement. Any help would be much appreciated. Thanks in advance 回答1: Well, you could use Linear Congruential Generator which, with proper selection of parameters, produce perfect mapping from u64 to u64. In other words, if you have access to previous 8bytes UUID, you could generate reasonable random next 8bytes UUID WITHOUT