Your histogram hist2 was not a pointer in your original code so it did not survive after the execution (see the references @Wile_E_Coyote sent you). DrawClone
creates a Clone of the histogram before drawing it, as a pointer … that’s why it survives. The simplest way to write your macro is:
void hw1(){
auto c = new TCanvas("c","TCanvas",10,10,800,800);
int npoints = 50000;
TRandom3 xgenerator, ygenerator;
xgenerator.SetSeed(0);
ygenerator.SetSeed(0);
auto hist2 = new TH2F("hist2","histogram",600,-3,3,600,-3,3);
for(int i=0; I<npoints ;i++){
float x = xgenerator.Uniform(-3,3);
float y = ygenerator.Uniform(-3,3);
hist2->Fill(x,y);
}
hist2->Draw("COLZ");
}