forked from lda/telodendria
Format source code.
This commit is contained in:
parent
ff879e715f
commit
fa88fc3323
9 changed files with 41 additions and 37 deletions
21
src/Rand.c
21
src/Rand.c
|
@ -29,24 +29,25 @@
|
|||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Generate random numbers using rejection sampling.
|
||||
* The basic idea is to "reroll" if a number happens to be
|
||||
* outside the range. However this could be extremely inefficient.
|
||||
/* Generate random numbers using rejection sampling. The basic idea is
|
||||
* to "reroll" if a number happens to be outside the range. However
|
||||
* this could be extremely inefficient.
|
||||
*
|
||||
* Another idea would just be to "reroll" if the generated number
|
||||
* ends up in the previously "biased" range, and THEN do a modulo.
|
||||
* Another idea would just be to "reroll" if the generated number ends up
|
||||
* in the previously "biased" range, and THEN do a modulo.
|
||||
*
|
||||
* This would be far more efficient for small values of max,
|
||||
* and fixes the bias issue. */
|
||||
* This would be far more efficient for small values of max, and fixes the
|
||||
* bias issue. */
|
||||
|
||||
/* This algorithm therefore computes N random numbers generally in
|
||||
* O(N) time, while being less biased. */
|
||||
/* This algorithm therefore computes N random numbers generally in O(N)
|
||||
* time, while being less biased. */
|
||||
void
|
||||
RandIntN(int *buf, size_t size, unsigned int max)
|
||||
{
|
||||
static pthread_mutex_t seedLock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static unsigned int seed = 0;
|
||||
int tmp;
|
||||
|
||||
/* Limit the range to banish all previously biased results */
|
||||
const int allowed = RAND_MAX - RAND_MAX % max;
|
||||
|
||||
|
@ -73,11 +74,13 @@ RandIntN(int * buf, size_t size, unsigned int max)
|
|||
}
|
||||
pthread_mutex_unlock(&seedLock);
|
||||
}
|
||||
|
||||
/* Generate just 1 random number */
|
||||
int
|
||||
RandInt(unsigned int max)
|
||||
{
|
||||
int val = 0;
|
||||
|
||||
RandIntN(&val, 1, max);
|
||||
return val;
|
||||
}
|
||||
|
|
|
@ -147,7 +147,8 @@ ROUTE_IMPL(RouteRefresh, args)
|
|||
/* Update the refresh token to point to the new access token */
|
||||
JsonValueFree(HashMapSet(DbJson(rtRef), "refreshes", JsonValueString(StrDuplicate(newAccessToken->string))));
|
||||
|
||||
/* Return the new access token and expiration timestamp to the client */
|
||||
/* Return the new access token and expiration timestamp to the
|
||||
* client */
|
||||
response = HashMapCreate();
|
||||
HashMapSet(response, "access_token", JsonValueString(StrDuplicate(newAccessToken->string)));
|
||||
HashMapSet(response, "expires_in_ms", JsonValueInteger(newAccessToken->lifetime));
|
||||
|
|
Loading…
Reference in a new issue