Use arc4random(3) in asteroids(1).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-07-19 21:10:05 +02:00
parent edb19f2394
commit c56daca682
1 changed files with 7 additions and 5 deletions

View File

@ -43,7 +43,7 @@ const float PI = 3.1415926532f;
inline float RandomFloat() inline float RandomFloat()
{ {
return (float) rand() / 32768.0f; return arc4random() / (float) UINT32_MAX;
} }
inline float RandomFloat(float min, float max) inline float RandomFloat(float min, float max)
@ -73,12 +73,14 @@ uint32_t starfield[STARFIELD_WIDTH * STARFIELD_HEIGHT];
void GenerateStarfield(uint32_t* bitmap, size_t width, size_t height) void GenerateStarfield(uint32_t* bitmap, size_t width, size_t height)
{ {
size_t numpixels = width * height; size_t numpixels = width * height;
for ( size_t i = 0; i < numpixels; i++ ) for ( size_t i = 0; i < numpixels; i++ )
{ {
uint8_t color = 0; uint8_t color = 0;
int randval = rand() % 256; uint8_t randval;
bool isstar = randval == 5 || randval == 42 || randval == 101; arc4random_buf(&randval, sizeof(randval));
if ( isstar ) { color = rand(); } if ( randval == 5 || randval == 42 || randval == 101 )
arc4random_buf(&color, sizeof(color));
bitmap[i] = MakeColor(color, color, color); bitmap[i] = MakeColor(color, color, color);
} }
} }
@ -467,7 +469,7 @@ Asteroid::Asteroid(Vector pos, Vector vel, float size, AsteroidType type)
} }
angle = 0.0f; angle = 0.0f;
turnspeed = DegreeToRadian(MAX_TURN_SPEED) * (RandomFloat() * 2.0f - 1.0f); turnspeed = DegreeToRadian(MAX_TURN_SPEED) * (RandomFloat() * 2.0f - 1.0f);
numpolygons = MIN_POLYS + rand() % (MAX_POLYS - MIN_POLYS); numpolygons = MIN_POLYS + arc4random_uniform(MAX_POLYS - MIN_POLYS);
slice = DegreeToRadian(360.0f) / (float) numpolygons; slice = DegreeToRadian(360.0f) / (float) numpolygons;
for ( size_t i = 0; i < numpolygons; i++ ) for ( size_t i = 0; i < numpolygons; i++ )
{ {