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

Randomized number selection question

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



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

Randomized number selection question
PostPosted: Fri Mar 28, 2008 12:01 pm     Reply with quote

Greetings fellow campers...

I have the need to generate a number, randomly, from 1 thru 5 (or 0 - 4). These numbers need to have a weight assigned to them so that, say, the number 1 is selected 40% of the time, 2 is selected 30% of the time, 3 is selected 15% of the time, 4 is selected 10% of the time and 5 is selected 5% of the time. So they all add up to being selected 100% of the time.

I'm not a wizzard at algorithms or mathematics so I'm asking for input on which road I should head down to accomplish this. It doesn't need to be numbers, it could also be routines that are selected.

Any ideas out there?

Thanks,
Ronald
-------------------------
If at first you don't succeed, try doing it the way your wife told you to.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Mar 28, 2008 12:44 pm     Reply with quote

I'm no math wiz tho.
I would do rand int 0-99thus making 1% chance for each number
then run result through a if statment.

if(rnd<40)//1 - 40% time
result=1;
break
if(rnd>39 && rnd <70)//2 - 30% time
result=2
break
and so on...
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Mar 28, 2008 8:52 pm     Reply with quote

Hola Ronald,

I'm agree with treitmey suggestion and I'm not a math wiz too.

Code:

#include <STDLIB.H>
#define RAND_MAX 100

int8 value, number;

void main()
{
   ........
   ........
   srand(1);
   ........
   ........

   while(1)
    {
    (int8)value = rand();
    number = 0;
    do
      {
       if((value >= 60)&&(value < 100)) // 40% chance to fall here
         {number = 1;  break;}
       if((value >= 30)&&(value < 60))  // 30%    "   "   "    "
         {number = 2;  break;}
       if((value >= 15)&&(value < 30))  // 15%    "   "   "    " 
         {number = 3;  break;}
       if((value >=  5)&&(value < 15))  // 10%    "   "   "    "
         {number = 4;  break;}
       if(value  < 5)                   // 5%     "   "   "    "
         {number = 5;  break;}
      }while(foo);
    ........
    ........ 
  }
}


Humberto
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Sat Mar 29, 2008 1:59 pm     Reply with quote

I think you need randmax = 99
0-99 gives you 100 ,... right

Is the randmax inclusive??

And since the rand num start with zero you could add one. rnd+=1
to get 1-100. That makes it a little easier to keep straight in your head.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sat Mar 29, 2008 9:39 pm     Reply with quote

Hi treitmey,

Quote:

I think you need randmax = 99

In the posted code, value never reach 100:
if((value >= 60)&&(value < 100))

Quote:

Is the randmax inclusive??

Yes, I think so.

Quote:

And since the rand num start with zero you could add one.

In the posted code the rand num never start with zero, did you see srand(1)?


Humberto
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Mar 31, 2008 7:53 am     Reply with quote

Sorry, perhaps I'm confused.
doesn't srand just seed the randomness?

I just was referencing 3.249 chm help file that says
for rand()
Quote:
The rand function returns a sequence of pseudo-random integers in the range of 0 to RAND_MAX.
After thinking about it. I think your code will work. We don't need 1% probability to start with. Just equal probability, then you limit the equal probability to a range of 100 which I think will give 1% in the end.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Mar 31, 2008 9:06 am     Reply with quote

Quote:

doesn't srand just seed the randomness?

Yes you are right. The point is that srand() was used widely in a PC environment with
an OS running behind, were you can use the function time() to get a quasi random seed.
In embedded C this is not available (still). Wink


Humberto
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Mar 31, 2008 3:03 pm     Reply with quote

I've never needed to use rand() before and it's giving me strange results.

I've defined rand_max as 100 and 20 but the result from rand() is in excess of rand_max. I'm just trying to simulate it with mplab and I'm not sure if this is a bug with mplab or what. Is mplab supposed to work with rand() properly?

Ronald
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Mar 31, 2008 3:39 pm     Reply with quote

Define the value of RAND_MAX before the including of stdlib.h, otherwise the code below will be valid and your max value will become 32767.
Code:
copied from stdlib.h

#ifndef RAND_MAX
#define RAND_MAX  32767    // The value of which is the maximum value
                           // ... returned by the rand function
#endif
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Mon Mar 31, 2008 4:11 pm     Reply with quote

Gotcha!
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