1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 /**
17 ************************************************************************
18 * @file M4PSW_Trace.c
19 * @brief Trace function for trace macros
20 * @note This file gives the implementation of the trace function used
21 * in the trace instrumentation macros
22 ************************************************************************
23 */
24
25 #include <stdio.h> /*for printf */
26 #include <stdarg.h> /* ANSI C macros and defs for variable args */
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "M4OSA_Types.h"
32 #include "M4OSA_Error.h"
33 #include "M4OSA_Memory.h"
34 #include "M4OSA_Mutex.h"
35 /**
36 ************************************************************************
37 * @fn M4OSA_ERR M4OSA_randInit()
38 * @brief this function initialize the number generator
39 * this function must be called once before any call to M4OSA_rand()
40 * need the stdlib and time libraries
41 * @note
42 * @param
43 * @return M4NO_ERROR
44 ************************************************************************
45 */
46
M4OSA_randInit()47 M4OSA_ERR M4OSA_randInit()
48 {
49 int i;
50
51 srand(time(NULL));
52
53 /* Windows' rand is rotten, the first generated value after the init
54 above is not random enough, so let's shake things a little... */
55
56 for (i=0; i<100; i++) rand();
57
58 return M4NO_ERROR;
59 }
60 /**
61 ************************************************************************
62 * @fn M4OSA_ERR M4OSA_rand(M4OSA_Int32* out_value, M4OSA_UInt32 max_value)
63 * @brief This function gives a random number between 1 and max_value
64 * (inclusive) with approximately equal probability, and
65 * returns this number in out_value. For instance, a max_value
66 * of 6 will simulate a fair 6-sided dice roll.
67 * @note
68 * @param out_value (OUT): on return, points to random result
69 * @param max_value (IN): max expected value
70 * @return M4NO_ERROR
71 ************************************************************************
72 */
73
M4OSA_rand(M4OSA_Int32 * out_value,M4OSA_UInt32 max_value)74 M4OSA_ERR M4OSA_rand(M4OSA_Int32* out_value, M4OSA_UInt32 max_value)
75 {
76 if( (out_value == M4OSA_NULL) || (max_value < 1) )
77 {
78 return M4ERR_PARAMETER;
79 }
80
81 (*out_value) = rand();
82 /* notice this algorithm will only work for max_values such that the multiplication
83 won't overflow, which means that max_value typically shouldn't go over the range of
84 an Int16. */
85 (*out_value) = (((*out_value) * max_value) / ((M4OSA_UInt32)RAND_MAX + 1)) + 1;
86
87 return M4NO_ERROR;
88 }
89
90
91