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