-5

I am referring to Python random modules' shuffle function.

from random import shuffle

list = [1, 2, 3, 4]

shuffle(list)

I would guess that above function uses random seed. I know that in C, rand function iterates over a few random seed numbers in computer. Therefore, when looping over the function, random function does not become random anymore.

Does shuffle function work similar to rand function in C? If so, how can I add my own seed that is random? (I am thinking of using time in millisecond to come up with unique value).

Previously posted comment on the accepted answer on this question but could not get any response (Shuffling a list of objects in python)

EDIT:

I want to make sure that random shuffle does not repeat its shuffling method in a loop. For example,

I want to shuffle [1, 2, 3, 4, 5, 6]

I loop over 10000 times.

It produces results below: [1, 3, 2, 4, 5, 6] [2, 1, 4, 5, 3, 6] ... (large number of different combinations of shuffling) [1, 3, 2, 4, 5, 6] [2, 1, 4, 5, 3, 6] ... (repeats the pattern).

I want to avoid above behaviour because I am looping over a large number. Would above behaviour happen in the first place? If so, do I have to change the seed after certain number of loop?

Community
  • 1
  • 1
seokhoonlee
  • 1,028
  • 12
  • 18
  • 2
    What do you mean by "iterates over a few random seed numbers in computer"? Are you asking if the output is deterministic? Are you asking whether it is "really" random or just pseudorandom? – BrenBarn May 05 '16 at 02:39
  • 2
    I answered your comment, but this question shows a lack of any research effort (thus the downvotes) - the exact behavior is clearly documented in the [`random` module](https://docs.python.org/3/library/random.html). – dimo414 May 05 '16 at 02:51
  • 1
    Your followup question is also answered in the documentation; the `random` module "*has a period of 2\*\*19937-1*", meaning it will output an identical sequence of values after that many steps. In case it's not clear, that's a **huge** number. For all practical purposes (except cryptography) it is indistinguishable from true randomness. – dimo414 May 05 '16 at 04:45

2 Answers2

3

Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-open range [0.0, 1.0). 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.

The random module also provides the SystemRandom class which uses the system function os.urandom() to generate random numbers from sources provided by the operating system.

Warning The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
3

I know that in C, rand function iterates over a few random seed numbers in computer.

No it doesn't. It uses whatever seed you set with srand, or 1 if you didn't call srand.

Does shuffle function work similar to rand function in C? If so, how can I add my own seed that is random? (I am thinking of using time in millisecond to come up with unique value).

Python's random module uses either system time or OS-provided randomness sources to seed the RNG:

random.seed([x])

Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

Changed in version 2.4: formerly, operating system resources were not used.

user2357112
  • 260,549
  • 28
  • 431
  • 505