Sign in to follow this  
musafir2007

Please help me understand this small code

Recommended Posts

Hi, I am trying to port a peice of code from C into python:

In C, this is the code:
[source]
int j = 0;
int i = 0;
float points[1024];

srand(117);

for (i = 0; i < 512; ++i)
{
points[j++] = (rand() / (float)RAND_MAX) * 768.0f - 256.0f;
points[j++] = (rand() / (float)RAND_MAX) * 768.0f - 256.0f;
}
[/source]

I don't really understand what is going on here. I know srand(117) initializes the random generator with seed 117, but I can't make sense of out the math.

In python, I would do something like this?

[source]
import random

points = []

random.seed(117)

for i in range(512):
points.append((random.random() / ???) * 768.0 - 256.0)
points.append((random.random() / ???) * 768.0 - 256.0)

[/source]

btw, this piece of code is taken from here: http://www.amanithvg.com/testsuite/amanithvg_sre/tests/rendering_rasterizer.inc

Please help

Share this post


Link to post
Share on other sites
RAND_MAX is the maximum value rand() can return, so (rand() / (float)RAND_MAX) gives you a random value in range [0.0-1.0]. So the whole thing gives you a number in range [-256, 512]. Why they do this I don't know. Also note that seeding the random generator with a fixed seed give you the same sequence of random numbers each time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this