classification
Title: Propose random.randbool()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: corona10, mark.dickinson, rhettinger, serhiy.storchaka, steven.daprano
Priority: normal Keywords:

Created on 2021-06-12 00:33 by corona10, last changed 2021-06-13 08:42 by rhettinger. This issue is now closed.

Messages (7)
msg395671 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-06-12 00:33
I noticed that the random library does not provide `random.randbool()`.
Generating bool value is quite common in the use-case when we generated faked data
(unittest, machine learning training, etc)

Somebody can say write your own library but it's too common use-case and in physically some isolated environments is hard to use 3rd party library.

Since the bool value is the built-in type of python, I think that is very useful when we provide this function.

I would like to get opinions from Raymond and then proceed with this issue.

Here is the candidate implementation:

def randbool():
    return bool(getrandbits(1))
msg395675 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-06-12 04:22
Not every one line expression needs to be a function in a library. `bool(getrandbits(1))` is self-explanatory enough, and it is doubtful that any implementation would be faster.

Using getrandbits(1) to return 0 or 1 is fine; if you need a bool, call bool on the result.

Aside: the name getrandbits is a bit sad, there's no setrandbits and we don't name the other random functions with a leading "get" prefix:

    getrandint(1, 6)


Raymond, would you consider providing an alias randbits and depreciating the getrandbits name? We don't have to remove it, just document it as depreciated to be removed in Python 5000 :-)
msg395679 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-12 06:50
It is difficult to deprecate getrandbits() because it is one of two base methods. All Random methods call either getrandbits() or randome(). Overriding them is a way to make a new random generator.
msg395686 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-06-12 13:13
To explain my thought,

> Not every one line expression needs to be a function in a library. `bool(getrandbits(1))` is self-explanatory enough,

Yeah, I agree with the point of view, it might be enough.

But considering the popularity of the Python language and there is a lot of new people who enter the programming world with Python for their own purpose so there are a lot of people who are not familiar with the concept of bits.
So I thought that the random module can become more friendly for those people.

And for example, Java/Scala already provides those high-level APIs (and there is a similar proposal at Go also: https://github.com/golang/go/issues/23804). but it does not mean that Python should provide the same APIs. And I know that we also have to consider the maintenance cost and the principle of library scope.

Anyway, this is the reason I proposed this feature and I want to hear the opinion from other core-devs :)

please let me know if I miss something or historical issue.
Enjoy your weekend.
msg395687 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-06-12 13:19
s / from other core-devs / from core-devs
msg395688 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-06-12 13:45
As for randbool() I concur with Steven. It is too trivial and in most case you do not even need to call bool().

Alternate methods:

    choice((False, True))
    random() < 0.5

They are less efficient than getrandbits(1), but can be easily extended to non-binary output or non-uniform distribution.
msg395733 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-06-13 08:42
> I would like to get opinions from Raymond and then 
> proceed with this issue.

I concur with Steven and Serhiy and don't think this should be added.
History
Date User Action Args
2021-06-13 08:42:12rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg395733

stage: resolved
2021-06-12 13:45:02serhiy.storchakasetmessages: + msg395688
2021-06-12 13:19:38corona10setmessages: + msg395687
2021-06-12 13:13:09corona10setmessages: + msg395686
2021-06-12 09:08:47mark.dickinsonsetnosy: + mark.dickinson
2021-06-12 06:50:49serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg395679
2021-06-12 04:22:51steven.dapranosetnosy: + steven.daprano
messages: + msg395675
2021-06-12 00:33:48corona10create