Weighted Random Sample works

How does the weighted random sampler selects the values

list(WeightedRandomSampler([10,5,10,5,10],10, replacement=True))
[0, 0, 0, 4, 4, 1, 2, 0, 4, 1]

why are there more no of zeros and 4s

WeightedRandomSampler will use torch.multinomial internally as shown here.

The passed weights will determine the weight to sample each index.
E.g. you can see that the returned indices will approximate the weights:

sampler = WeightedRandomSampler([10,5,10,5,10],1000, replacement=True)
indices = []
for idx in sampler:
    indices.append(idx)

print(torch.tensor(indices).unique(return_counts=True))
> (tensor([0, 1, 2, 3, 4]), tensor([280, 124, 230, 137, 229]))