gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
torch get 100 random numbers from 0 to 1
- User
torch get 100 random numbers from 0 to 1
- Assistant
To generate 100 random numbers from 0 to 1 using PyTorch, you can make use of the
torch.randfunction. This function creates a tensor filled with random numbers from a uniform distribution on the interval ([0, 1)). Here's how you can do it:pythonimport torch # Generate 100 random numbers between 0 and 1 random_numbers = torch.rand(100) print(random_numbers)This code will create a 1D tensor with 100 elements, each being a random number between 0 and 1. If you need these numbers in a different shape, you can specify the desired shape in the
torch.randfunction, for example,torch.rand(10, 10)for a 10x10 matrix of random numbers.