I have got a pwm output for which I generate the duty value randomly using rand(). I currently set the rand_max value like this in my header file:
#define RAND_MAX 1023
#include <stdlib.h>
However, I would like to be able to adjust the maximum value from a POT, and then ultimately the minimum value. I'm just wondering if anyone can give me any advice on the best way to do this, or point me in the direction of any resources?
Thanks!
RLScott
Joined: 10 Jul 2007 Posts: 465
Re: Random Numbers
Posted: Mon Mar 17, 2008 11:00 am
Well, a nice linear function of the original random number would serve the purpose. Supoose your POT is read by an A/D input into a number, val, from 0 to 255. Then you could generate a random number in the range of 0...val with:
Code:
int32 k;
int16 kScaled;
k = <Random Number from 0 to 1023>
kScaled = (k * val) >> 10;
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum