Skip to content

Commit

Permalink
Allow rng to accept parameters in swapped order
Browse files Browse the repository at this point in the history
  • Loading branch information
oddbjol committed Jun 26, 2013
1 parent ca2d29a commit 5dc2f67
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions rng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
#include "rng.h"
#include <stdlib.h>

long rng(long low, long high)
long rng(long val1, long val2)
{
return low + long((high - low + 1) * double(rand() / double(RAND_MAX + 1.0)));
// Input order of parameters doesn't matter, we just swap things around if val1 is higher than val2.
if(val1 <= val2)
{
return val1 + long((val2 - val1 + 1) * double(rand() / double(RAND_MAX + 1.0)));
}
else
{
return val2 + long((val1 - val2 + 1) * double(rand() / double(RAND_MAX + 1.0)));
}
}

bool one_in(int chance)
Expand Down
2 changes: 1 addition & 1 deletion rng.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _RNG_H_
#define _RNG_H_

long rng(long low, long high);
long rng(long val1, long val2);
bool one_in(int chance);
bool x_in_y(double x, double y);
int dice(int number, int sides);
Expand Down

0 comments on commit 5dc2f67

Please sign in to comment.