Tasos
Tasos

Reputation: 7577

Replicate a T-SQL random function in Python

I have a T-SQL code and I want to run a few simulations in Python. There is a code that includes random functions and I am not sure how I can replicate it.

When I have RAND() in SQL, I just use this in Python:

import random as random
print random.random()

But, I have also this code: RAND(CHECKSUM(NEWID()))

I guess, it is used for some kind of seed in the RAND function. But, how I can replicate the same thing in Python to have as much closer results I could?

Upvotes: 0

Views: 96

Answers (1)

Cato
Cato

Reputation: 3701

in Python you need to first call

random.seed() in you program (once only)

then

random.random()

each time you want a random number

to give a pseudo random number from 0 to 1


yes RAND(CHECKSUM(NEWID())) appears to be a good trick to get a random value to use as a seed - it could be argued that a NEWID has a greater amount of randomness than using time as a seed. It depends on your application, time is considered insufficient as a seed for cryptography without adding other entropy - the python randomize uses time as a seed

Upvotes: 1

Related Questions