How do I create a list of random numbers without duplicates?

前端 未结 17 2408
灰色年华
灰色年华 2020-11-22 13:30

I tried using random.randint(0, 100), but some numbers were the same. Is there a method/module to create a list unique random numbers?

Note: The fol

17条回答
  •  执笔经年
    2020-11-22 13:45

    to sample integers without replacement between minval and maxval:

    import numpy as np
    
    minval, maxval, n_samples = -50, 50, 10
    generator = np.random.default_rng(seed=0)
    samples = generator.permutation(np.arange(minval, maxval))[:n_samples]
    
    # or, if minval is 0,
    samples = generator.permutation(maxval)[:n_samples]
    

    with jax:

    import jax
    
    minval, maxval, n_samples = -50, 50, 10
    key = jax.random.PRNGKey(seed=0)
    samples = jax.random.shuffle(key, jax.numpy.arange(minval, maxval))[:n_samples]
    

提交回复
热议问题