using random.choice() with preference and uniqueness

  • Last Update :
  • Techknowledgy :

If using NumPy is an option for you, this is quite straightforward to implement:

np.random.choice(['yes', 'no', 'unknown'], p = [0.15, 0.15, 0.7])

If you wanted 100 choices with exactly 70 'unknown' entries and 30 'yes' or 'no' choices:

hundred_choices = ['unknown'] * 70 + [random.choice(['yes', 'no']) for _ in range(30)]

To get exactly some percentage of the values written you could use the shuffle() function on a pre-generated list that contains the number of items you need.

choices = ['unknown'] * 70 + ['yes'] * 15 + ['no'] * 15
random.shuffle(choices)

If you want 70% of probability for 'unknown':

decisions = 70 * ['unknown'] + 15 * ['yes'] + 15 * ['no']
choice(decisions)

If you want exactly 70% of 'unknown's:

from random
import choice, shuffle

values = 70 * ['unknown'] # your 70 unknowns
for i in range(30):
   values.append(choice(['yes', 'no'])) # fill the rest randomly
shuffle(values) # shuffle

for x in values:
   file.write('{}\n', x)

Suggestion : 2

A class for generating random numbers using the highest-quality sources provided by the operating system. See random.SystemRandom for additional details.,secrets — Generate secure random numbers for managing secrets Random numbers Generating tokens How many bytes should tokens use? Other functions Recipes and best practices ,In particular, secrets should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography.,The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

>>> token_bytes(16)
b '\xebr\x17D*t\xae\xd4\xe3S\xb6\xe2\xebP1\x8b'
>>> token_hex(16)
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
>>> token_urlsafe(16)
'Drmhze6EPcv0fN_81Bj-nA'
import string
import secrets
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(8))
import string
import secrets
alphabet = string.ascii_letters + string.digits
while True:
   password = ''.join(secrets.choice(alphabet) for i in range(10))
if (any(c.islower() for c in password) and any(c.isupper() for c in password) and sum(c.isdigit() for c in password) >= 3):
   break
import secrets
# On standard Linux systems, use a convenient dictionary file.
# Other platforms may need to provide their own word - list.
with open('/usr/share/dict/words') as f:
   words = [word.strip() for word in f]
password = ' '.join(secrets.choice(words) for i in range(4))

Suggestion : 3

Generate Random string with Uppercase letters and digits using random.choice(),Now we will look at the methods through which we can genarate random string with upper case letters and digits.,Generate Random string with Uppercase letters and digits Using random.choices(),In this article, we will learn to generate a random string with upper case letters and digits in Python. Also we will be learning about ASCII encoding format which will be used in one of the method to generate a random string with upper case letters and digits.

EXAMPLE :

import string
import random

# initialized a empty list in which
# random string will be appended.
randomcharlst = []

# Arguments in range() will print desired length of
   # string(uppercase alphabets + digits).
# Here length should be of 10 chars.
for i in range(0, 10):
   randomcharlst.append(random.choice(string.ascii_uppercase + string.digits))

randomStr = ''.join(randomcharlst)

print(randomStr)

OUTPUT :

1 YRBJO9D2K

Suggestion : 4

Now retrieve a random sample of 10 items, but choose the items in proportion to their prices. For example, an item that is twice the price of another would be twice as likely to appear in the query results:,Retrieve a uniform random sample of 10 items:, Compute a random value between 0 and 99. If the random number is 0 to 1, this query produces a random number from 0 to 100: select cast (random() * 100 as int); int4 ------ 24 (1 row) ,This example uses the SET command to set a SEED value so that RANDOM generates a predictable sequence of numbers.

Syntax

RANDOM()

Compute a random value between 0 and 99. If the random number is 0 to 1, this query produces a random number from 0 to 100:

select cast(random() * 100 as int);

int4
-- -- --
24
   (1 row)

Retrieve a uniform random sample of 10 items:

select *
   from sales
order by random()
limit 10;

First, return three RANDOM integers without setting the SEED value first:

select cast(random() * 100 as int);
int4
-- -- --
6
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
68
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
56
   (1 row)

Now, set the SEED value to .25, and return three more RANDOM numbers:

set seed to .25;
select cast(random() * 100 as int);
int4
-- -- --
21
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
79
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
12
   (1 row)

Compute a random value between 0 and 99. If the random number is 0 to 1, this query produces a random number from 0 to 100:

select cast(random() * 100 as int);

int4
-- -- --
24
   (1 row)

Retrieve a uniform random sample of 10 items:

select *
   from sales
order by random()
limit 10;

Now retrieve a random sample of 10 items, but choose the items in proportion to their prices. For example, an item that is twice the price of another would be twice as likely to appear in the query results:

select *
   from sales
order by log(1 - random()) / pricepaid
limit 10;

First, return three RANDOM integers without setting the SEED value first:

select cast(random() * 100 as int);
int4
-- -- --
6
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
68
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
56
   (1 row)

Now, set the SEED value to .25, and return three more RANDOM numbers:

set seed to .25;
select cast(random() * 100 as int);
int4
-- -- --
21
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
79
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
12
   (1 row)

Finally, reset the SEED value to .25, and verify that RANDOM returns the same results as the previous three calls:

set seed to .25;
select cast(random() * 100 as int);
int4
-- -- --
21
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
79
   (1 row)

select cast(random() * 100 as int);
int4
-- -- --
12
   (1 row)