-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrandom.cpp
More file actions
94 lines (78 loc) · 2.66 KB
/
Copy pathrandom.cpp
File metadata and controls
94 lines (78 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
******************************************************************************
* File Name : random.c
* Author : Xavier Halgand
* Date :
* Description :
******************************************************************************
*/
//-------------------------------------------------------------------------------------------------------
#include "random.h"
//-------------------------------------------------------------------------------------------------------
/* RNG handler declaration */
RNG_HandleTypeDef RngHandle;
//-------------------------------------------------------------------------------------------------------
void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
{
/* RNG Peripheral clock enable */
__RNG_CLK_ENABLE();
__HAL_RNG_ENABLE(&RngHandle);
}
//-------------------------------------------------------------------------------------------------------
/**
* @brief RNG MSP De-Initialization
* This function freeze the hardware resources used in this example:
* - Disable the Peripheral's clock
* @param hrng: RNG handle pointer
* @retval None
*/
void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
{
/* Enable RNG reset state */
__RNG_FORCE_RESET();
/* Release RNG from reset state */
__RNG_RELEASE_RESET();
}
//---------------------------------------------------------------------------
void randomGen_init(void)
{
/* Configure the RNG peripheral #######################################*/
RngHandle.Instance = RNG;
uint32_t random32bit;
if(HAL_RNG_Init(&RngHandle) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
}
HAL_RNG_GenerateRandomNumber(&RngHandle, &random32bit);
srand(random32bit);
}
//---------------------------------------------------------------------------
/**************
* returns a random float between a and b
*****************/
float_t frand_a_b(float_t a, float_t b)
{
return ( rand()/(float_t)RAND_MAX ) * (b-a) + a ;
}
//---------------------------------------------------------------------------
/**************
* returns a random float between 0 and 1
*****************/
float_t randomNum(void)
{
float_t random = 1.0f;
uint32_t random32bit;
HAL_RNG_GenerateRandomNumber(&RngHandle, &random32bit);
random = (float_t) (random32bit / 4294967294.0f);
return random;
}
/*-----------------------------------------------------------------------------*/
/**************
* returns a random integer between 0 and MIDI_MAX
*****************/
uint8_t MIDIrandVal(void)
{
return (uint8_t)lrintf(frand_a_b(0 , MIDI_MAX));
}
//-------------------------------------------------------------------------------------------------------