# uuid.UUID(bytes=x.bytes) form where bytes are passed in by r



## shiva (Apr 23, 2014)

We should use uuid.UUID(bytes=x.bytes) form where bytes are passed in by reading from random() which in Python/FreeBSD is hooked to /xxx/random]  We do generate CSRF token per user session. Could you please suggest me how to do in the Python using random from FreeBSD related.?!


http://svn.python.org/projects/python/b ... ib/uuid.py


----------



## shiva (Apr 26, 2014)

*Re: uuid.UUID(bytes=x.bytes) form where bytes are passed in*


```
x=ramdom.randint(1,10000)
uuid.UUID(bytes=x.bytes)
```
Getting this kind of error:

```
AttributeError: 'int' object has no attribute 'bytes'
```


----------



## obsigna (Apr 26, 2014)

*Re: uuid.UUID(bytes=x.bytes) form where bytes are passed in*


```
import sys, random, uuid
uuid.UUID(bytes = "%016x" % (random.randint(0, sys.maxint)))
```


----------



## shiva (Apr 27, 2014)

*Re: uuid.UUID(bytes=x.bytes) form where bytes are passed in*

Thank you @obsigna.


----------



## shiva (Apr 28, 2014)

*Re: uuid.UUID(bytes=x.bytes) form where bytes are passed in*


```
import sys, random, uuid
uuid.UUID(bytes = "%016x" % (random.randint(0, sys.maxint)))
```
Is this meets requirement of 128 bits. Also how do I need to test whether this meets 128bits??


----------



## obsigna (Apr 28, 2014)

*Re: uuid.UUID(bytes=x.bytes) form where bytes are passed in*

RTM: Python Documentation: 9.6. random — Generate pseudo-random numbers.



> ... 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. ...



So the randomness of the calls to the random object is at best 53 bits. If this does not meet your requirements, then use os.urandom().


```
import os, uuid
uuid.UUID(bytes = os.urandom(16))
```

However, you still need to explain to yourself why you choose random.randint(1,10000) in the first place.


----------



## shiva (Apr 28, 2014)

*Re: uuid.UUID(bytes=x.bytes) form where bytes are passed in*

Thanks a lot @obsigna... I'm very new to Python also these kind of issues.


----------

