Comments (7)
What range would you like to have? 16 bits on 8bitters already seems to be plenty!
from mozzi.
I'd like to have 0...255 or -128...127 on 8 bit and 0...65535 or -32768 ... 32767 on 16 bit. The simplest solution would be something like
union _secret { uint8_t u; int8_t i; } secret;
uint8_t randu8() { return xorshift() & 0xFF; }
int8_t randi8() { _secret.u = randu8(); return _secret.i; }
... and similarly for 16 bit. [I'm sure there's a C++ convention for bit-converting unsigned to signed but I don't know what it is].
from mozzi.
I don't understand, why not using
int8_t rand(int8_t minval, int8_t maxval);
int rand(int minval, int maxval);
which are respectively 8 and 16 bits on AVR.
from mozzi.
Because the range of rand is minval to maxval-1, not to maxval.
from mozzi.
Ah, I get it. Well, this is coherent with Arduino's random()
function.
I am not at the origins of these but if you look at the implementation this is done to avoid type promotion. Of course, it would be quite easy to add overloaded functions without maxval
as a parameter which would return in the full type range, for instance (untested):
int8_t rand()
{
return (int8_t) lowByte(xorshift96());
}
Welcome to submit a PR ;), otherwise will try to do it soon⦠If @tfry-git and @sensorium are not against this could go in.
Edit: I guess this version would not work as the compiler will not know which one to use between the different return types, with no parameter, hence the name rand()
can probably not be used, but rand_uint8
could.
(Still wondering if having the last value, which will not pop-up very frequently indeed, is actually of any use, but thsese "bare" functions would be faster which sounds appealing).
from mozzi.
Maybe we don't really need all of these. Essentially, all these would do is call xorshift96() and then cast to a type that needs to be named, explicitly, anyway. (Note that the existing overloads perform additional computation with a given precision, and therefore these do make sense, indeed).
Brainstorming some ideas:
- we could create a parameter-less
long rand()
simply as a friendly-sounding alias toxorshift96()
- bit-casting between signed and unsigned is the only really cumbersome exercise, so maybe have one
randUnsigned()
and onerandSigned()
variant for convenience (the latter, again, being a mere alias toxorshift96()
). - isn't this a great opportunity for templating?
rand<int8_t>()
, etc. Except of course, it may be overkill.
And then of course, while we're at it, I suppose it will make sense to convert "int" and "long" to int16_t and int32_t?
from mozzi.
isn't this a great opportunity for templating? rand<int8_t>(), etc. Except of course, it may be overkill.
And to port that for direct, U/SFixMath
random numbers :P ?
from mozzi.
Related Issues (20)
- AVR PWM range is not actually 0-488 ("8.5 bit"), but effectively 0-400 HOT 6
- Minor / infrastructure: Avoid duplicate workflow runs on pushes to PRs HOT 1
- I2S dac with a nano. HOT 12
- VS10XX as DAC HOT 38
- RFC: Analog input scaling - what's the best approach HOT 3
- Allow to disable twi_nonblock HOT 5
- Review uses of "int", and "long" data types.
- Optimization of `Oscil::phMod()`
- GP0 io issues on RP2040 HOT 7
- Force PWM mode for power supply on GPIO, or suggest it in documentation
- I2S dacs config in Mozzi2 HOT 9
- A question, not an issue per se: lgt8f328p HOT 2
- RP2040 is out of tune HOT 5
- URL to webpage in _About_ section is wrong HOT 2
- Port for PY32f002 - PY32F003 -PY32F030? HOT 2
- HTML styling not rendering in the Github's Readme HOT 8
- Teensy (3.6 and 4.1) CI jobs are failing HOT 1
- AudioInput does not compile with Mozzi 2.0
- ESP32 21:58:07.296 -> E (200) ADC: CONFLICT! driver_ng is not allowed to be used with the legacy driver HOT 3
- Mozzi not working on STM32 HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mozzi.