Plan 9 from Bell Labs’s /usr/web/sources/plan9/sys/src/ape/lib/v/nrand.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <stdlib.h>

#define	MASK	0x7FFFFFFFL
#define	FRACT	(1.0 / (MASK + 1.0))

extern long lrand(void);

double
frand(void)
{

	return lrand() * FRACT;
}

nrand(int n)
{
	long slop, v;

	slop = MASK % n;
	do
		v = lrand();
	while(v <= slop);
	return v % n;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].