I typically use either
(torch.rand((2, 5)) < 0.25).float()
or
torch.full((2, 5), 0.25).bernoulli_()
So technically, the second saves one step (rand + compare + float vs. full + bernoulli), but I haven’t really seen this as the bottleneck in anything I do, so it entirely depends on my mood - whether the glass is half full or half rand.
Best regards
Thomas