dimanche 21 janvier 2018

Selecting multiple & random values with limit

I have this code here:

$ids = implode(',', array_rand(array_column($test, 'id', 'id'), 5));

Here's what that code above does:

"First extract the id column indexing also by the id, then pick 5 random ones, and finally implode into a comma separated list. Since keys must be unique, this has the added benefit of not returning duplicate ids if there happen to be duplicates in the array" this is from my previous question

Now if I change my $arr from my old question to this:

Array
(
    [id] => 13
    [pets] => 8
    [num_of_times] => 3
)
Array
(
    [id] => 15
    [pets] => 8
    [num_of_times] => 6
)
Array
(
    [id] => 16
    [pets] => 10
    [num_of_times] => 2
)
Array
(
    [id] => 17
    [pets] => 9
    [num_of_times] => 4
)
Array
(
    [id] => 18
    [pets] => 10
    [num_of_times] => 3
)
Array
(
    [id] => 19
    [pets] => 10
    [num_of_times] => 10
)
Array
(
    [id] => 20
    [pets] => 0
    [num_of_times] => 11
)
Array
(
    [id] => 21
    [pets] => 8
    [num_of_times] => 9
)
Array
(
    [id] => 22
    [pets] => 9
    [num_of_times] => 0
)
Array
(
    [id] => 23
    [pets] => 4
    [num_of_times] => 3
)
Array
(
    [id] => 24
    [pets] => 0
    [num_of_times] => 1
)
Array
(
    [id] => 40
    [pets] => 8
    [num_of_times] => 0
)
Array
(
    [id] => 43
    [pets] => 2
    [num_of_times] => 2
)

num_of_times is the number of times that id or "user" can be selected.

So if I had a for loop like this:

for ($i = 1; $i <= 10; $i++) {
    $ids = implode(',', array_rand(array_column($arr, 'id', 'id'), 5));
    echo $ids;
}

how can I make sure, for example, the first array with id 13 does NOT go into $ids more than 3 times but CAN go into $ids 3 times OR less, when in the for loop? (this applies for all the id's)

For example, the final result would be something like this:

13,15,17,19,23
13,21,22,40,43
13,15,17,19,23
15,23,24,40,43 // 13 cannot be selected anymore because it already hit the "num_of_times" limit which is 3 for the number 13. Same thing for all the other numbers/id's
...
...
...
...
...
...




JavaScript: Does Math.Random() use the current timestamp to generate random numbers?

Follow up, if I manage to send two requests at EXACTLY the same time, does Math.Random() generate the same number?




Random number between 1000000 and 9999999 if 7 is entered

As the title suggests, I'm looking for a way of generating a Random number between 1000000 and 9999999 if 7 is entered and similarly if any other number is entered




sampling uniformly (x,y,z) such that x+y+z=0

I am trying to sample uniformly from the set of all point (x,y,z) such that x+y+z = 0 and -1<=x<=1, -1<=y<=1 and -1<=z<=1.

My idea was the following: I sampled uniformly from the 6 dimensional simplex(following this suggestion), i.e. from the set of points (a,b,c,d,e,f) such that a+b+c+d+e+f= 0 and 0<=a<=1, 0<=b<=1, 0<=c<=1, 0<=d<=1, 0<=e<=1 and 0<=f<=1. Since geometrically the set of all points satifying (1) is a hexagon with vertices (-1,1,0), (-1,0,1), (0,-1,1), (1,-1,0), (1,0,-1) and (0,1,-1), I computed (x,y,z) = a*(-1,1,0)+b*(-1,0,1)+c*(0,-1,1)+d*(1,-1,0)+e*(1,0,-1)+f*(0,1,-1). I sampled half a million points following this method, but unfortunatly, it seems like the points are not uniformly distributed.

Here is a plot

Does anybody know what the problem is with this and how to correct it?




How to make a function that picks according to a changing distribution, without passing over and over?

This is not a question about MatLab, but it is a question about how to achieve something that would be easy in object-oriented programming when you're using a less sophisticated language.

I'm a mathematician who is writing some MatLab code to test an algorithm in linear algebra (I won't burden you with the details). The beginning of the program is to generate a random 500 by 50 matrix of floats (call it A). In the course of running my program, I will want to pick random rows of A, not uniformly at random, but rather according to a distribution where the likelihood of row i being picked is different, depending on the specific matrix that has been generated.

I want to write a function called "pickRandomRow" that I can call over and over when I need it. It will use the same probability distribution on the rows throughout each individual run of the program, but that distribution will change between runs of the program (because the random matrix will be different).

If I were using a more object-oriented language than MatLab, I would make a class called "rowPicker" which could be initialized with the information about the specific random matrix I'm using on this run. But here, I'm not sure how to make a function in MatLab that can know the information it needs to know about the random matrix A once and for all, without passing A to the function over and over (expensively), when it's not changing.

Possible options

  • Make pickRandomRow a script instead of a function, so it can see the workspace. Then I wouldn't be able to give pickRandomRow any arguments, but so far I don't see why I'd need to.
  • Start messing areound with classes in MatLab.



Can I use a single `default_random_engine` to create multiple normally distributed sets of numbers?

I want to generate a set of unit vectors (for any arbitrary dimension), which are evenly distributed across all directions. For this I generate normally distributed numbers for each vector component and scale the result by the inverse of the magnitude.

My question: Can I use a single std::default_random_engine to generate numbers for all components of my vector or does every component require its own engine?

Afaik, each component needs to be Gaussian-distributed independently for the math to work out and I cannot assess the difference between the two scenarios. Here's a MWE with a single RNG (allocation and normalization of vectors is omitted here).

std::vector<std::vector<double>> GenerateUnitVecs(size_t dimension, size_t count)
{
    std::vector<std::vector<double>> result;

    /* Set up a _single_ RNG */
    size_t seed = GetSeed(); // system_clock
    std::default_random_engine gen(seed);
    std::normal_distribution<double> distribution(0.0, 1.0); 

    /* Generate _multiple_ (independent?) distributions */
    for(size_t ii = 0; ii < count; ++ii){
        std::vector<double> vec;
        for(size_t comp = 0; comp < dimension; ++comp)
            vec.push_back(distribution(gen)); // <-- random number goes here

        result.push_back(vec);
    }
    return result;
}

Thank you.




How do you generate more than 100000 UNIQUE url's in C++

So currently I'm working on an assignment and one of the steps involved is to generate. Generating alphanumerical, integers and string would not be a problem for me. But this being an URL, I have no idea on how to start on this program.