random-sample

Storing the results of a prepared statement as a table in mysql?

若如初见. 提交于 2019-12-05 14:23:46
Is it possible to store the result of a prepared table in mysql ? My use case is - : I am creating two variables based on certain conditions of the source table, then fetching the randomized rows, based on this criteria. Since I have 10 of such tables, should I be 1st joining them and then doing this randomization on the "overall" passing/filtering criteria (See also @total below, which is my main criteria, PER table) set @total=(select count(*) from tab_1 where predict_var ="4" or predict_var ="2" ) ; set @sample= ( select @total*(70/30)) ; PREPARE STMT FROM " SELECT * FROM tab_1 WHERE

Can `rand()` in c++ be used to generate unbiased bools?

不问归期 提交于 2019-12-05 10:59:36
I have written the following function bool random_bool(double probability) { double p_scaled = probability * (RAND_MAX+1) - rand(); if ( p_scaled >= 1 ) return true; if ( p_scaled <= 0 ) return false; return random_bool( p_scaled ); } Given, that rand() generates a number from uniform distribution on {0,1,...,RAND_MAX-1,RAND_MAX} and numbers from subsequent calls can be treated as independent for all practical purposes except cryptography, this should return true with probability p : two if statements return true with probability slightly below p , and false with the probability slightly above

Generate string for Regex pattern in Ruby

廉价感情. 提交于 2019-12-04 18:02:05
问题 In Python language I find rstr that can generate a string for a regex pattern. Or in Python we have this method that can return range of string: re.sre_parse.parse(pattern) #..... ('range', (97, 122)) .... But In Ruby I didn't find any thing. So how to generate string for a regex pattern in Ruby(reverse regex)? I wanna to some thing like this: "/[a-z0-9]+/".example #tvvd "/[a-z0-9]+/".example #yt "/[a-z0-9]+/".example #bgdf6 "/[a-z0-9]+/".example #564fb "/[a-z0-9]+/" is my input. The outputs

Generating random numbers on open-open interval (0,1) efficiently

时光毁灭记忆、已成空白 提交于 2019-12-04 08:46:36
I'm looking for an efficient way to generate random floating-point numbers on the open-open interval (0,1). I currently have an RNG that generates random integers on the closed-closed interval of [0, (2^32)-1]. I've already created a half-open floating point RNG on the interval [0,1) by simply multiplying my result from the integer RNG by 1/((2^32)-1) rather than dividing by (2^32)-1 since it's inefficient. The way I'm currently going about generating numbers on the interval (0,1) is with a conditional statement like the one below: float open_open_flt = (closed_open_flt==0) ? closed_open_flt :

How can I get exactly n random lines from a file with Perl?

混江龙づ霸主 提交于 2019-12-04 06:32:04
Following up on this question, I need to get exactly n lines at random out of a file (or stdin ). This would be similar to head or tail , except I want some from the middle. Now, other than looping over the file with the solutions to the linked question, what's the best way to get exactly n lines in one run? For reference, I tried this: #!/usr/bin/perl -w use strict; my $ratio = shift; print $ratio, "\n"; while () { print if ((int rand $ratio) == 1); } where $ratio is the rough percentage of lines I want. For instance, if I want 1 in 10 lines: random_select 10 a.list However, this doesn't give

Randomly associate elements of two vectors given conditions

徘徊边缘 提交于 2019-12-04 05:01:29
问题 I have a data.table of capitals capitals<-data.table(capital=c(100,50,25,5)) capitals capital 1: 100 2: 50 3: 25 4: 5 and a data.table of losses losses<-data.table(loss=c(45,10,5,1)) losses loss 1: 45 2: 10 3: 5 4: 1 I would like to randomly associate each capital with a loss (without replacement) such that the loss is less than or equal to the capital. In pseudo code one possible implementation would be Set all capitalLoss to NA (i.e. capitals[, capitalLoss:=NA]) Order losses from largest to

Iterative or Lazy Reservoir Sampling

删除回忆录丶 提交于 2019-12-04 04:12:35
I'm fairly well acquainted with using Reservoir Sampling to sample from a set of undetermined length in a single pass over the data. One limitation of this approach, in my mind, is that it still requires a pass over the entire data set before any results can be returned. Conceptually this makes sense, since one has to allow items in the entirety of the sequence the opportunity to replace previously encountered items to achieve a uniform sample. Is there a way to be able to yield some random results before the entire sequence has been evaluated? I'm thinking of the kind of lazy approach that

SQL random sample with groups

喜夏-厌秋 提交于 2019-12-04 03:50:20
I have a university graduate database and would like to extract a random sample of data of around 1000 records. I want to ensure the sample is representative of the population so would like to include the same proportions of courses eg I could do this using the following: select top 500 id from degree where coursecode = 1 order by newid() union select top 300 id from degree where coursecode = 2 order by newid() union select top 200 id from degree where coursecode = 3 order by newid() but we have hundreds of courses codes so this would be time consuming and I would like to be able to reuse this

Temporal correlations when employing System.Random (not present when employing System.Random.TF)

我是研究僧i 提交于 2019-12-04 03:27:52
问题 This question concerns the origins of temporal correlations one observes with System.Random when one generates successive randoms from successive seeds (where one discards the same number of generators for each seed). In Using mkStdGen from System.Random to generate random booleans Answer 1 and Using mkStdGen from System.Random to generate random booleans Answer 2 it was suggested (based on the reddit article referenced theirin) that the first few generators should be discarded in order to

R-How to generate random sample of a discrete random variables?

…衆ロ難τιáo~ 提交于 2019-12-03 23:25:39
This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 5 years ago . Learn more . In R, I want to generate a random sample of a discrete random variable: X , where: P(X=a)=P(X=-a)=1/2 . I have been searching for a function online, but there seems no direct function doing this. I think you are looking to generate samples of a Bernoulli random variable. A Bernoulli random variable is a special case of a binomial random variable. Therefore, you can try rbinom(N,1,p) . This will generate N samples, with value 1 with probability p , value 0 with