Genreating ranom number wihtout repetition

I have written this code to generate random numbers. The code work as expected but I am trying to store the value in a variable using list comprehension. Any help in rearranging the code to fit in one list comprehension. Reason why I want to in list compr because it is faster considering the large range of list.

import numpy as np
import random

a = np.arange(10)
n = .8
train_len = int(len(a) * n)

rand_train = [ i for i in range(len(a))]
for _ in range(1):
    print(random.sample(rand_train, k=train_len))

Here is what I did. Used different approach. If you have another way to approach this do not hesitate to share your code.

my_list = set(np.random.choice(len(a), train_len, replace=False))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.