[c594489] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[c594489] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[c594489] | 35 | #include <stdlib.h>
|
---|
| 36 |
|
---|
[c718bda] | 37 | static int glbl_seed = 1;
|
---|
[c594489] | 38 |
|
---|
[c718bda] | 39 | int rand(void)
|
---|
[c594489] | 40 | {
|
---|
[ff98ce8] | 41 | return glbl_seed = ((1366 * glbl_seed + 150889) % RAND_MAX);
|
---|
[c594489] | 42 | }
|
---|
| 43 |
|
---|
[c718bda] | 44 | void srand(unsigned int seed)
|
---|
[c594489] | 45 | {
|
---|
[ff98ce8] | 46 | glbl_seed = seed % RAND_MAX;
|
---|
[c594489] | 47 | }
|
---|
[b2951e2] | 48 |
|
---|
[8338a81] | 49 | /** Compute quotient and remainder of int division.
|
---|
| 50 | *
|
---|
| 51 | * @param numer Numerator
|
---|
| 52 | * @param denom Denominator
|
---|
| 53 | * @return Structure containing quotient and remainder
|
---|
| 54 | */
|
---|
| 55 | div_t div(int numer, int denom)
|
---|
| 56 | {
|
---|
| 57 | div_t d;
|
---|
| 58 |
|
---|
| 59 | d.quot = numer / denom;
|
---|
| 60 | d.rem = numer % denom;
|
---|
| 61 |
|
---|
| 62 | return d;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /** Compute quotient and remainder of long division.
|
---|
| 66 | *
|
---|
| 67 | * @param numer Numerator
|
---|
| 68 | * @param denom Denominator
|
---|
| 69 | * @return Structure containing quotient and remainder
|
---|
| 70 | */
|
---|
| 71 | ldiv_t ldiv(long numer, long denom)
|
---|
| 72 | {
|
---|
| 73 | ldiv_t d;
|
---|
| 74 |
|
---|
| 75 | d.quot = numer / denom;
|
---|
| 76 | d.rem = numer % denom;
|
---|
| 77 |
|
---|
| 78 | return d;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /** Compute quotient and remainder of long long division.
|
---|
| 82 | *
|
---|
| 83 | * @param numer Numerator
|
---|
| 84 | * @param denom Denominator
|
---|
| 85 | * @return Structure containing quotient and remainder
|
---|
| 86 | */
|
---|
| 87 | lldiv_t lldiv(long long numer, long long denom)
|
---|
| 88 | {
|
---|
| 89 | lldiv_t d;
|
---|
| 90 |
|
---|
| 91 | d.quot = numer / denom;
|
---|
| 92 | d.rem = numer % denom;
|
---|
| 93 |
|
---|
| 94 | return d;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[fadd381] | 97 | /** @}
|
---|
[b2951e2] | 98 | */
|
---|