Random.getstate function Python

0

I need your help in the following situation:

  • I have a loop that every random interaction is generated from 0 to 400.

  • I need my function to return at what point in the loop the X number was generated. (For example, loop 4 generated the number 15 and so on ...)

  • In my studies, I saw the random.getstate and random.setstate roles, but I do not know how to use them or if there is a better solution that they.

I ask for your help.

Thank you in advance.

    
asked by anonymous 03.03.2017 / 00:46

1 answer

0

If I understand correctly this is what you need:

from random import randint

tamanholoop=10

for i in xrange(tamanholoop):
    print "Posicao " + str(i)
    print "Randomico "+ str(randint(0,400))

The result looks something like this:

C:\Python27>python randon.py
Posicao 0
Randomico 56
Posicao 1
Randomico 58
Posicao 2
Randomico 287
Posicao 3
Randomico 374
Posicao 4
Randomico 134
Posicao 5
Randomico 332
Posicao 6
Randomico 94
Posicao 7
Randomico 291
Posicao 8
Randomico 228
Posicao 9
Randomico 325

Just print the position of the loop and just below print the random value I get in the randint function at the interval you need!

    
03.03.2017 / 01:45