dimanche 2 août 2020

j-easy/easy-random : Support for Bidirectional Association

How can I configure j-easy/easy-random for Bidirectional association? For example,

Parent contains Set<Child> Child has reference to Parent

In Set<Child>, every child instance should refer to the same object of the composing Parent. However, currently new instance of Parent is created for every Child in Set<Child>




samedi 1 août 2020

Randomize and compare matrices in R

I have two matrices (1 and 2) which I multiply, resulting in the concordance matrix (c).

I need to randomize one of theses matrices 1000 times and them compare the number of times the value of these concordance matrices were higher than the first comparation.

# Exemple:
 mat1 <- matrix(rbinom(222, 1, 0.5),nrow=74,ncol=3) # habitat

 mat2 <- matrix(rbinom(592, 1, 0.5),nrow=74,ncol=8) # modular

 tmat1 <- t(mat1)

 # Multipying the tranposed matrix 1 by matrix 2:

 c <- tmat1%*%mat2 # concordance matrix c

 resu <- matrix(NA,nrow=3,ncol=8) # creating the matrix to fill with the results of values higher than c

  for(r in 1:1000) {

mat3 <- apply(mat1,1,sample) # randomizing matrix 1, maintaining the number of intercations by nodes

cc <- mat3%*%mat2 # multiplying these matrices 

for (i in 1:dim(c)[1]){ # rows

 for(j in 1:dim(c)[2]){ # columns

   if(cc[i]>=c[j]){ #  

   resu[i,j] <- sum(cc[i]>=c[j]) # filling the matrix with the number of times cc was higher then c

   } 

 }

}

}

But the result is wrong: it is resulting in a matrix with correct number of rows (3) and columns (8) but all fill out by 1.

Each element has to be a different number of times the concordance was higher in cc[i] than c[j].

Any thoughts, please?




How to shuffle an array without moving FALSY elements?

Here is my attempt; a slightly-modified Fisher-Yates algorithm. I am not sure how to make sure it's random though.

const shuffleWithoutMovingFalsies = array => {
  const newArray = [...array];
  const getRandomValue = (i, N) => ~~(Math.random() * (N - i) + i);
  newArray.forEach((elem, i, arr, j = getRandomValue(i, arr.length)) => arr[i] && arr[j] && ([arr[i], arr[j]] = [arr[j], arr[i]]));
  return newArray;
}

const array = [1, 2, null, 3, null, null, 4, 5, 6, null];

const shuffledArray = shuffleWithoutMovingFalsies(array);

console.log(shuffledArray);

All I did was add arr[i] && arr[j] && as a check to make sure both elements about to be swapped are NOT falsy.




Random() function for n values but results must equal 100 Delphi 7

Pseudocode Example:

Random function: (1 to 5, results less than 100, results must equal 100 when all random numbers are 
added).

Showmessage:

Number 1 = 35
Number 2 = 15
Number 3 = 10
Number 4 = 20

Number 5 = 20




PySpark, generate random integer for row not in list

I am trying to efficiently add an integer column to a dataframe, where the integer is any number, i, from 0 to N, where i is not in the set R. The set r in R for every row is obtained by a mapping from the value of the first column, word1, to a set, K. Every value in word1 has a map to a set of values within R.

word1     | word2     | word3
Idaho       New York    <rand integer based on word 1 map>

I'm trying to do this without a UDF. This is the psuedo-code I've landed on but I really don't know if this is even possible with PySpark. I'm stuck in how to generate a random integer within a range that excludes values. Normally, I'd generate random values until a value meeting the condition is met. But I think I'd need to move any while loop into a udf, which I'm trying to avoid.

df_expanded = df.withColumn('word3', rand() where rand() not in map(col('word1')))

Here is how I'd write it in a udf, but this is very slow.

def get_sample(all_ii, word):
    # the set of integers to exclude
    s = iid[word]
    #  all_ii, set, has all possible integers in a range
    a = all_ii - s
    if len(a): return random.sample(a, 1)[0]
    return ''



generating gamma random variables in matlab

I need to know if we have a array of scale and shape parameters and want to generate n (it could be 10^6 for instance) gamma random variables for each elements of pair scale and shape parameters array but without a loop or for, then what can we do? Is there any trick because apparently there is no way at least in matlab helps? thanks in advance...




Generating random time with intervals of 5 minutes

I'm trying to get an output that looks like that:

12:15
07:55
02:20
04:35

I managed to get the following output:

2020-07-03 03:53:32
2020-07-20 08:01:15
2020-07-29 10:04:11
2020-07-07 07:17:24
2020-07-20 12:13:32

by using this code:

import datetime
import time
import random

MINTIME = datetime.datetime(2020,7,1,0,0,0)
MAXTIME = datetime.datetime(2020,7,31,0,0,0)

mintime_ts = int(time.mktime(MINTIME.timetuple())) #convert date into int
maxtime_ts = int(time.mktime(MAXTIME.timetuple())) #convert date into int

for RECORD in range(100):
    random_ts = random.randint(mintime_ts, maxtime_ts)
    RANDOMTIME = datetime.datetime.fromtimestamp(random_ts)
    print(RANDOMTIME)

So, basically I have two issues. The first is I want to get the time in format of (hh:mm) only. The second is that I want the step to be 5 minutes (something like 14:50 or 04:35 .. etc).

Thanks