Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RIOT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cm-projects
RIOT
Commits
9c93e72d
Commit
9c93e72d
authored
9 years ago
by
Kaspar Schleiser
Browse files
Options
Downloads
Patches
Plain Diff
sys: random: add simple LCG PRNGs
parent
fab16817
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
Makefile.pseudomodules
+3
-0
3 additions, 0 deletions
Makefile.pseudomodules
sys/random/Makefile
+13
-0
13 additions, 0 deletions
sys/random/Makefile
sys/random/minstd.c
+73
-0
73 additions, 0 deletions
sys/random/minstd.c
sys/random/musl_lcg.c
+45
-0
45 additions, 0 deletions
sys/random/musl_lcg.c
with
134 additions
and
0 deletions
Makefile.pseudomodules
+
3
−
0
View file @
9c93e72d
...
...
@@ -23,3 +23,6 @@ PSEUDOMODULES += netif
# include variants of the AT86RF2xx drivers as pseudo modules
PSEUDOMODULES
+=
at86rf23%
PSEUDOMODULES
+=
at86rf21%
# add all pseudo random number generator variants as pseudomodules
PSEUDOMODULES
+=
prng_%
This diff is collapsed.
Click to expand it.
sys/random/Makefile
+
13
−
0
View file @
9c93e72d
ifeq
(,$(filter prng_%,$(USEMODULE)))
USEMODULE
+=
prng_mersenne
endif
ifneq
(,$(filter prng_mersenne,$(USEMODULE)))
SRC
+=
mersenne.c
endif
ifneq
(,$(filter prng_minstd,$(USEMODULE)))
SRC
+=
minstd.c
endif
ifneq
(,$(filter prng_musl_lcg,$(USEMODULE)))
SRC
+=
musl_lcg.c
endif
include
$(RIOTBASE)/Makefile.base
This diff is collapsed.
Click to expand it.
sys/random/minstd.c
0 → 100644
+
73
−
0
View file @
9c93e72d
/**
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
* Code taken from C FAQ (http://c-faq.com/lib/rand.html).
*/
/**
* @ingroup sys_random
* @{
* @file
*
* @brief Simple Park & Miller "minimal standard" PRNG
*
* This file contains a simple Park-Miller pseudo random number generator.
*
* While not very random when considering crypto requirements, this is probably
* random enough anywhere where pseudo-randomness is sufficient, e.g., when
* provided with a sensible seed source, for MAC algorithms.
*
* The implementation is taken from the C FAQ, but modified to use magic number
* division and adapted to RIOT's coding conventions..
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @}
*/
#include
<stdint.h>
#include
"div.h"
#define a 48271
#define m 2147483647
#define q (m / a)
/* 44488 */
#define r (m % a)
/* 3399 */
static
uint32_t
_seed
=
1
;
int
rand_minstd
(
void
)
{
uint32_t
hi
=
div_u32_by_44488
(
_seed
);
uint32_t
lo
=
div_u32_mod_44488
(
_seed
);
uint32_t
test
=
(
a
*
lo
)
-
(
r
*
hi
);
if
(
test
>
0
)
{
_seed
=
test
;
}
else
{
_seed
=
test
+
m
;
}
return
_seed
;
}
uint32_t
genrand_uint32
(
void
)
{
/* minstd as implemented returns only values from 1 to 2147483647,
* so run it two times to get 32bits */
uint16_t
A
=
(
rand_minstd
()
>>
15
);
uint16_t
B
=
(
rand_minstd
()
>>
15
);
return
(((
uint32_t
)
A
)
<<
16
)
|
B
;
}
void
genrand_init
(
uint32_t
val
)
{
if
(
!
val
)
{
val
=
1
;
}
_seed
=
val
;
}
This diff is collapsed.
Click to expand it.
sys/random/musl_lcg.c
0 → 100644
+
45
−
0
View file @
9c93e72d
/**
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
* Code taken from Musl C library. Original copyright notice:
*
* Copyright © 2005-2014 Rich Felker, et al.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include
<stdint.h>
static
uint64_t
_seed
;
void
genrand_init
(
uint32_t
s
)
{
_seed
=
s
-
1
;
}
uint32_t
genrand_uint32
(
void
)
{
_seed
=
6364136223846793005ULL
*
_seed
+
1
;
return
_seed
>>
32
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment