CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Random Numbers

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jemly



Joined: 13 May 2007
Posts: 34

View user's profile Send private message

Random Numbers
PostPosted: Mon Mar 17, 2008 10:44 am     Reply with quote

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

View user's profile Send private message

Re: Random Numbers
PostPosted: Mon Mar 17, 2008 11:00 am     Reply with quote

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;


Robert Scott
Real-Time Specialties
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group