|
1 /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
|
2 |
|
3 /* |
|
4 Part of the Wiring project - http://wiring.org.co |
|
5 Copyright (c) 2004-06 Hernando Barragan |
|
6 Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/ |
|
7 |
|
8 This library is free software; you can redistribute it and/or |
|
9 modify it under the terms of the GNU Lesser General Public |
|
10 License as published by the Free Software Foundation; either |
|
11 version 2.1 of the License, or (at your option) any later version. |
|
12 |
|
13 This library is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 Lesser General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU Lesser General |
|
19 Public License along with this library; if not, write to the |
|
20 Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
21 Boston, MA 02111-1307 USA |
|
22 |
|
23 $Id$ |
|
24 */ |
|
25 |
|
26 extern "C" { |
|
27 #include "stdlib.h" |
|
28 } |
|
29 |
|
30 void randomSeed(unsigned int seed) |
|
31 { |
|
32 if (seed != 0) { |
|
33 srandom(seed); |
|
34 } |
|
35 } |
|
36 |
|
37 long random(long howbig) |
|
38 { |
|
39 if (howbig == 0) { |
|
40 return 0; |
|
41 } |
|
42 return random() % howbig; |
|
43 } |
|
44 |
|
45 long random(long howsmall, long howbig) |
|
46 { |
|
47 if (howsmall >= howbig) { |
|
48 return howsmall; |
|
49 } |
|
50 long diff = howbig - howsmall; |
|
51 return random(diff) + howsmall; |
|
52 } |
|
53 |
|
54 long map(long x, long in_min, long in_max, long out_min, long out_max) |
|
55 { |
|
56 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
|
57 } |
|
58 |
|
59 unsigned int makeWord(unsigned int w) { return w; } |
|
60 unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } |