Python Random.getstate function

Asked

Viewed 100 times

0

I need your help in the following situation:

  • I have a loop that generates a random value from 0 to 400.

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

  • I don’t put any example code, because I don’t know how to do this.

  • In my studies, I saw that there is a function Random.getstate and Random.setstate, but I don’t know how to use them or if there’s a better solution than them.

I ask for your help.

Thanks in advance.

1 answer

0


If I understand right 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 is 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 caught in the function randint in the interval you need!

  • Perfect! Just what I needed. Thank you very much. .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.