• ### What is your GameDev Story?

Public Group

This topic is 2719 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

##### 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 on other sites
Thanks,
but how can I know the RAND_MAX for random generator in python? also is RAND_MAX affected by the seed?

##### Share on other sites
Look at the documentation for the range of values returned by the random function. For rand in C, its from 0 to RAND_MAX, for example, which is why you divide by RAND_MAX to get a random number from 0 to 1.

##### Share on other sites
oh ok, got it! so I don't actually need to divide by RAND_MAX in python.
I can just do random.random() which will return the number between 0 and 1.

Thanks!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015830
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!