2

I'm trying to understand principle of random library's work. I consider some situations: 1) I have value b and it can take the value 0 or 1 with probability 0.5. I know, that I can realize that with

numpy.random.choice(numpy.arange(0, 2), p=[0.5, 0.5])

I want to understand, what steps are executed to choose some value? It separates this segment and gets random value? How does it happen?

2) Or if I have, maybe a list. lst = [1, 4, 7, 3, 254, 6, 2, 7, 3123, 454657, 34, 565, 99] I can you a common way to do that random.choice(lst)) but I want to understand too, what occurs during a decision?

3
  • in choice() you have list with 15 elements so random use random.randint(15) to choose element - lst[ random.randint( len(lst) ) ] Commented Nov 17, 2016 at 9:36
  • Read the source? Commented Nov 17, 2016 at 9:39
  • if you have probability [0.25, 0.75] then you choose from list [0, 1, 1, 1] Commented Nov 17, 2016 at 9:39

2 Answers 2

4

You should try opening the module if you want a full understanding of how it works. To do this you can use, in a python interpreter:

>>>import numpy
>>>help(numpy)

This will show some information on the document. You can find the function you are wondering about and read the code to understand how it works. I hope this helps.

Some more complex information on numpy is on this link aswell: https://docs.scipy.org/doc/numpy-1.10.1/reference/

Remember, that it is not real random. Just pseudo random. The code uses a pseudo random number generator to generate random numbers.

Sign up to request clarification or add additional context in comments.

9 Comments

Does that really show you the code? For choice, all this tells me is "Choose a random element from a non-empty sequence", which doesn't help.
Yes.It should. It works for me when I use IDLE Python Shell.
I'm using IDLE Python Shell as well. Tried both Python 3 and Python 2. Doesn't show me the code.
I added a link which contains the whole file.
I hope it helps
|
2

Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.Python doesn't actually generate random numbers: it gets them from the operating system, which has a special driver that gathers entropy from various real-world sources, such as variations in timing between keystrokes and disk seeks. More info can be found in the python documentation.

1 Comment

Them here is numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.