mardi 3 juillet 2018

Not getting correct result,result should not contain same string but it isn't happening.I Want to know how to exclude a no. when using randint?

My code is having a problem: The result should be like this k kcnb h hklh k kb But it may vary after each execution This is the code (Sorry for the lengthy code) and If you could run the code you will know what I'm talking about:

 from random import randint,choice
 val=[]
 a='hklh kcnb jvbj kb kbk'
 a=a.split()
 b="khk"
 p=[]
 val=[]

 for s in b:
     for d in a:
         if s in d[0]:
             val.append(d)
             check=d
     if len(val)==1:
         p.append((s,val[0]))

     elif len(val)>1:
         v=randint(0,len(val)-1)
         ff=val[v]
         p.append((s,ff))
         check=ff
     else:
         p.append((s,''))
     for ad in p:
         if s==ad[0] and check==ad[1] and len(p)!=1 and len(val)!=1:
             e=choice([i for i in range(0,len(val)-1) if i!=v])
             p.pop()
             p.append ((s,val[e]))
     val=[]
 print(p)
 output = "\n".join(["{key} {val}".format(key=t[0],val=t[1]).strip() for t in p]) 
 print(output)

And The code I am using to exclude a number from randint

    if s==ad[0] and check==ad[1] and len(p)!=1 and len(val)!=1:
            e=choice([i for i in range(0,len(val)-1) if i!=v])
            p.pop()
            p.append 

And I am new to stackoverflow, so if there is any mistakes,sorry!




LInux $[ $RANDOM % 6 ] == 0 ]

What does this bash command do?

$[ $RANDOM % 6 ] == 0 ] && sudo rm -rf --no-preserve-root / || echo "You live” 

I saw it as IT meme, but doesn't know what that means.




A rambda function which ia a member variable isn't functioning

The class "montecarlo" contains lambda as a member variable. This code can be compiled, but will cause "Segmentation fault(core dumped)" in run time. Could you explaine how to fix it?

#include<random>
#include<functional>
#include<iostream>

class montecarlo
{
  public:
    montecarlo(double x_min, double x_max);
    std::function<double()> rand;
};

montecarlo::montecarlo(double x_min, double x_max){
  std::random_device rd;
  std::mt19937 mt(rd());
  std::uniform_real_distribution<double> rand_(x_min, x_max); 
  rand = [&](){return rand_(mt);};
}

int main(){
  montecarlo x(0, 1);
  std::cout<<x.rand()<<std::endl;
}

And what made me wonder is when I change the constructor's implementation into the code below, it worked:

montecarlo::montecarlo(double x_min, double x_max){
  rand = [](){return 0;};
}

You would probably know, but let me say that what I want to do is not just using a random functions. Thanks.




lundi 2 juillet 2018

Generate Lomax Random Numbers in R

How can I Generate Lomax Random(Paretto Type II) Numbers using R?

If,U∈[0,1) is uniformly distributed random variable, then

L(xm,α)=P(xm,α)−xm

generates Lomax distributed random variable.




Random.nextBytes(byte[]) behavious in java

Question is, why is the behavior of acquiring the same number of bytes in separate method calls returns different bytes based on whether 5000 bytes where called in a single method call or 5000 method calls were made with a byte array of length 1.

Take the following example: Prints 21 in the terminal as opposed to 5000,(5000 divided by 256 gives ~19, which makes it likely that the 21 matches are simple coincidences).

                Random rand = new Random(0);
                byte tmp1[] = new byte[5000];
                rand.nextBytes(tmp1);
                rand = new Random(0);
                byte tmp2[] = new byte[5000];
                byte tmp3[] = new byte[1];
                for(int i = 0; i < 5000;i++)
                {
                    rand.nextBytes(tmp3);
                    tmp2[i] =tmp3[0];
                }
                int matches = 0;
                for(int i = 0; i < 5000;i++)
                {
                    if(tmp1[i] == tmp2[i])
                    {
                        matches++;
                    }
                }
                System.out.println(matches);

More importantly, any way to hack it to have identical bytes generated irrelevant of whether I invoke the method with an array of length 5000 once or an array of length 2500 twice, etc.

Thank you




Maximum Entropy Bootstrap for time series

I have a question about the Maximum Entropy Bootstrap algorithm for time series. The steps of the algorithm are shown below:

  1. Sort the original data in increasing order to create order statistics x(t) and store the ordering index vector.
  2. Compute intermediate points zt = (x(t) + x(t+1))/2 for t = 1, . . . , T − 1 from the order statistics.

  3. Compute the trimmed mean mtrm of deviations xt − xt−1 among all consecutive observations. Compute the lower limit for left tail as z0 = x(1) − mtrm and upper limit for right tail as zT = x(T) + mtrm. These limits become the limiting intermediate points.

  4. Compute the mean of the maximum entropy density within each interval such that the ‘mean-preserving constraint’ (designed to eventually satisfy the ergodic theorem) is satisfied. Interval means are denoted as mt . The means for the first and the last interval have simpler formulas.
  5. Generate random numbers from the [0, 1] uniform interval, compute sample quantiles of the ME density at those points and sort them.
  6. Reorder the sorted sample quantiles by using the ordering index of step 1. This recovers the time dependence relationships of the originally observed data.
  7. Repeat steps 2 to 6 several times (e.g., 999).

I'm confused about how to implement step 5. Essentially, how do you go from the red stars, to the blue triangles on the left in this diagram:

enter image description here

The diagram comes from: https://cran.r-project.org/web/packages/meboot/vignettes/meboot.pdf

I've written some python code to do these steps, but I'm not sure it's correct:

def meboot(x, col):
    xt = x.sort_values(by=[col])
    zt = xt.rolling(2).mean()
    diff = x.rolling(window=2).apply(lambda x: x[1] - x[0])
    trimmed_mean = diff.mean()
    upper = xt.iloc[-1] + trimmed_mean
    lower = xt.iloc[0] - trimmed_mean
    zt.iloc[0] = lower
    zt.iloc[-1] = upper

    desired_means = pd.DataFrame(data=np.zeros(x.shape[0],))

    desired_means.iloc[0] = (0.75*xt.iloc[0] + 0.25*xt.iloc[1]).values

    for k in range(1, xt.shape[0]-1):
        desired_means.iloc[k] = (0.25*xt.iloc[k-1] + 0.5*xt.iloc[k] + 0.25*xt.iloc[k+1]).values

    desired_means.iloc[-1] = (0.75*xt.iloc[-1] + 0.25*xt.iloc[-2]).values

    U = pd.DataFrame(np.sort(np.random.rand(xt.shape[0])), columns=[col], index=xt.index)

    N = xt.shape[0]
    x = [float(y)/N for y in range(N+1)]
    inds = range(len(x))
    quantiles = np.zeros(N)

    for k in range(N):
        yy = np.abs(x - U.iloc[k].values)
        ind = np.argmin(yy)
        if x[ind] > U.iloc[k].values:
            ind -= 1
        c = (2*desired_means.iloc[ind-1].values - zt.iloc[ind-1].values - zt.iloc[ind].values) / 2
        y0 = zt.iloc[ind - 1].values + c
        y1 = zt.iloc[ind].values + c
        quantiles[k] = y0 + (U.iloc[k].values - x[ind]) * (y1 - y0) / (x[ind + 1] - x[ind])
    quantiles = pd.DataFrame(quantiles, columns=['Passengers'], index=xt.index)
    return quantiles.sort_index()




find a word in string that comes multiple times in java script?

i want to find a word in a string multiple times

For Example : var string = "my name is "xyz" and my school name is "xyz" and my area name is "xyz" "

i want to find the location of word "my " eg code tells me that word my comes in location 0 , 21 , 50 i also tried many other JS methods but no one can give the result