random

rand() seeding with time() problem

人走茶凉 提交于 2020-01-04 04:06:05
问题 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] =

Cannot shuffle list in Python

假如想象 提交于 2020-01-04 04:03:10
问题 This is my list: biglist = [ {'title':'U2','link':'u2.com'}, {'title':'beatles','link':'beatles.com'} ] print random.shuffle(biglist) that doesn't work! It returns none. 回答1: random.shuffle shuffles the list, it does not return a new list. So check biglist , not the result of random.shuffle . Documentation for the random module: http://docs.python.org/library/random.html 来源: https://stackoverflow.com/questions/1530161/cannot-shuffle-list-in-python

Random number that averages a particular number

假如想象 提交于 2020-01-04 04:02:11
问题 Seems simple, but I'd like a formula (.net preferably) which: For a given number- say, 1.5 - the formula will output a random number which taken over a series will average around 1.5... so it could be 0.1, 1.2, 7.1, 2.5, .2, etc, but the average value will be close to 1.5. clarification: I would like the numbers to be positive. 回答1: public class RandomAroundAverage { Random r = new Random(); public double Random(double middle, double scale) { return r.NextDouble() * scale - (scale / 2) +

Random integers from a function always return the same number - why?

耗尽温柔 提交于 2020-01-04 03:51:16
问题 I'm currently learning C++ and have a question about the random_device. I'm trying to simulate the monty hall problem. For this I need random integers to pick random doors. Therefore I tried making a function that returns random integers for this purpose. int guessGetRandomIndex() { std::random_device rd; // obtain a random number from hardware std::mt19937 eng(rd()); // seed the generator std::uniform_int_distribution<> distr(0, 2); return distr(eng); However when I try to get random

Using same random number generator across multiple functions

天涯浪子 提交于 2020-01-04 03:50:09
问题 I am given to believe that random number generators (RNGs) should only be seeded once to ensure that the distribution of results is as intended. I am writing a Monte Carlo simulation in C++ which consists of a main function ("A") calling another function ("B") several times, where a large quantity of random numbers is generated in B. Currently, I am doing the following in B: void B(){ std::array<int, std::mt19937::state_size> seed_data; std::random_device r; std::generate(seed_data.begin(),

LIMIT then RAND rather than RAND then LIMIT

こ雲淡風輕ζ 提交于 2020-01-04 02:54:21
问题 I'm using full text search to pull rows. I order the rows based on score (ORDER BY SCORE) , then of the top 20 rows (LIMIT 20), I want to rand (RAND) the result set. So for any specific search term, I want to randomly show 5 of the top 20 results. My workaround is code based- where I put the top 20 into an array then randomly select 5. Is there sql way to do this? 回答1: You can do this using an inner select. Select the top twenty rows in the inner select. In the outer select order these rows

Numeric, reversible, pseudorandom number generator, for very large numbers

和自甴很熟 提交于 2020-01-04 02:41:29
问题 Does anyone know of an algorithm that fits all of the above criteria? I need to specify a seed number, and a range that I want the output numbers to fall under (which will also be the range that the input numbers are in). This function also needs to have a counterpart that reverses the operation. for example: I pass the seed 5, and the range 5-35, then I receive the number 27. I can then pass this into a function that reverses the operation, using the same range, that will give me the number

How to get consistent behavior from C++ <random>

大城市里の小女人 提交于 2020-01-04 02:20:37
问题 I'm running into an issue where if I reseed a random number generator from the C++ <random> library, I sometimes get an upcoming value from the sequence as the first sample. After that first sample, I get a repeatable sequence. There seems to be a pattern to it, but I can't quite work out what it is. Minimal example: #include <iostream> #include <random> using namespace std; int main(){ mt19937 engine {1}; normal_distribution<float> nd {0, 1}; for (int i=0; i<10; i++){ for (int j=0; j<=i; j++

random使用方法及实例

大兔子大兔子 提交于 2020-01-04 02:09:42
import random print ( random . randint ( 1 , 10 ) ) # 产生 1 到 10 的一个整数型随机数 print ( random . random ( ) ) # 产生 0 到 1 之间的随机浮点数 print ( random . uniform ( 1.1 , 5.4 ) ) # 产生 1.1 到 5.4 之间的随机浮点数,区间可以不是整数 print ( random . choice ( 'tomorrow' ) ) # 从序列中随机选取一个元素 print ( random . randrange ( 1 , 100 , 2 ) ) # 生成从1到100的间隔为2的随机整数 a = [ 1 , 3 , 5 , 6 , 7 ] # 将序列a中的元素顺序打乱 random . shuffle ( a ) print ( a ) #生成10个元素的列表 a = [ random . randint ( 1 , 100 ) for _ in range ( 10 ) ] print ( a ) for _ in range(n) 一般仅仅用于循环n次,不用设置变量,用 _ 指代临时变量,只在这个语句中使用一次。 在循环中两者的作用相似 参考链接: 1 来源: CSDN 作者: soda東風 链接: https://blog.csdn

R: How to add a column with a randomly chosen value from each row of a matrix?

落花浮王杯 提交于 2020-01-04 01:29:09
问题 I'll preface this by saying I'm an R noob and that I think this may have an easy solution, but I'm struggling to find it. I've got a matrix with 2 columns and 1,000 rows. Keeping the rows fixed, I'd like to create a new variable that randomly chooses one of the elements from the 2 columns. For example making a simple matrix: matrix(c(1,1,4,6,1,3,2,1,1,7), ncol=2) [,1] [,2] [,3] [1,] 1 3 3 [2,] 1 2 1 [3,] 4 1 4 [4,] 6 1 1 [5,] 1 7 7 In the simplified matrix above, the 3rd column (which I just