dimanche 2 mai 2021

random sample for from dataset with multiple appointment per patient

I have a dataset with multiple appointments per patient, I need to generate a dataset with only single appointment per patient. In my dataset I have 50000 appointments and only 32400 patients. Any idea what code to use in R ??




Why do random number generators cause problems when executed in parallel?

I have an assignment and the following was mentioned:

Parallelism adds further complications, as the random number generator function must behave correctly when two or more threads call it simultaneously, i.e., it must be thread-safe.

However, I can't understand why that is a problem in the first place. Random generators are usually called with a seed as a parameter and output a number by doing multiple operations on it, I understand that we need each thread to use a different seed but other than that where does the problem come from? I have realized that spawning and calling the random generator in a parallel region instead of a serial one also really worsens performance but I can't understand why this would happen as by the looks of it, the random number generator should be parallelizable with no problems since they are no dependencies.

Any help in understand the theory behind this would be appreciated.




How to generate random quantites until reach a certain number

"i want to generate random numbers (with limits for each case(item number)) until reach specific number (which is the sum of the total column) "

;this is a a daily sales list for a restaurant, I want to ask the code to generate quantities that will lead to specific totals sales, the unit price column is fixed

this code is working but i cant choose specific value to reach but i have to choose range, and the other problem is when th range difference is small the code take too much time to solve the problem and it still working ..."

table present code

Sub mad()

Do Until Range("A54").Value > Range("j54").Value And Range("A54").Value < Range("j55").Value

For i = 11 To 51
Select Case i
Case 12, 14, 15, 16, 33 To 36
'Small and medium meal'
Cells(i, 3).Value = Int((VBA.Rnd * 12) + 3)

Case 11, 13, 17, 37
'big meals'
Cells(i, 3).Value = Int((VBA.Rnd * 6) + 1)

Case 20 To 28
'sandwitches and snacks'
Cells(i, 3).Value = Int((VBA.Rnd * 4) + 1)

Case 29 To 32, 38 To 40, 45 To 47
'small snacks, potato, cheese'
Cells(i, 3).Value = Int((VBA.Rnd * 3) + 0)

Case 41
'cola 250 ml '
Cells(i, 3).Value = Int((VBA.Rnd * 15) + 1)

Case 42 To 44
'cola 1 & 2 ltr'
Cells(i, 3).Value = Int((VBA.Rnd * 4) + 0)

Case Else
'addtitions'
Cells(i, 3).Value = Int((VBA.Rnd * 3) + 0)
End Select



Next i

Loop

End Sub



samedi 1 mai 2021

Random sample from set of observations with no repeat

I have a dataset of 50000 observations related to patients hospital appointment. i need to create a random sample dataset with only one single appointment for each patient. In my original dataset some patients had 12 appointments, however, for my analysis i need to include one appointment per patient. When i analysed my dataset there was 324100 unique patients ID which mean there were only 324100 patients who had 50000 appointments. So , i need a dataset with random 324100 appointments, one appointment per patient only. Any idea ??




How to generate gamma distributed random number in C++? [duplicate]

I am trying to convert some R codes into C++ in order to use in Rcpp package. I am a complete beginner in C++.

I am trying to understand how to generate gamma distributed random number in C++ for a given set of shape and scale parameter.

For example, in R I can use:

rgamma(n = 1, shape = a, scale = b)

What is an equivalent code in C++?




How to get random numbers from a range?

I want to be able to have the loop print different random numbers each time I press the number that corresponds to the range I want. However, with what I have below, what happens is that.

I press 2 ----> gives me $16 and subtracts 10 mins.

I press 2 again ----> gives me $16 again and subtracts 10 mins from my initialTime.

What I want for it to do is that even if I press 2 again, it gives me another random number from the range I gave. Below is my code.

#include <cstdlib>
#include <ctime>  
#include <iostream>

int main(int argc, char** argv) 
{
    srand((unsigned)time(0));
    int groupNumber1,initialMoney1, initialTime1, teensMoney, familyMoney, teensTime, familyTime;
    initialMoney1 = 0;
    initialTime1 = 30;
    teensMoney = 1 + rand() % ((5 + 1) - 1);
    familyMoney = 4 + rand() % ((25 + 1) - 4);
    teensTime =  2 + rand() % ((4 + 1) -2);
    familyTime =  6 + rand() % ((12 + 1) - 6);

for (int i=0; i<5; i++)
{
    system("CLS");
    cout<<name1<<", choose what group to serve. \n" "Choose wisely. \n"
        "1 - A group of teenagers \n" 
        "2 - A family \n"

    cout<<'\n';
    cout<<"Your choice: ";
    cin>>groupNumber1;
    cout<<'\n';
    cout<<"You chose group number "<<groupNumber1<<"."<<endl;
    cout<<'\n';

        if ((groupNumber1 == 1))
            {
                cout<<"You now have a total of "<<"$"<< (initialMoney1 += teensMoney)<<" and have "<< ( initialTime1 -= teensTime) <<" minutes remaining."<<endl;   
            }
        else if ((groupNumber1 == 2))
            {
                cout<<"You now have a total of "<<"$"<< (initialMoney1 += familyMoney) <<" and have "<< (initialTime1 -= familyTime) <<" minutes remaining."<<endl;  
            }           



Stata: sortseed gets resetted

Although I set sortseed at the beginning of my Stata sessions, it gets resetted everytime Stata needs to break the tie in sort command. Is there a way to set sortseed for the whole session?