from typing import Optional
import torch


def create_feature_from_index(index: int, dim: int, dtype: Optional[torch.dtype] = torch.float32) -> torch.Tensor:
    """
    Fix the torch random generator seed based on
    `index` and return a randomly sampled value
    of appropriate dtype to ensure reproducibility.
    """
    return torch.rand(dim, generator=torch.Generator().manual_seed(index), dtype=dtype)