View previous topic :: View next topic |
Author |
Message |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
Randomized number selection question |
Posted: Fri Mar 28, 2008 12:01 pm |
|
|
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
|
|
Posted: Fri Mar 28, 2008 12:44 pm |
|
|
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
|
|
Posted: Fri Mar 28, 2008 8:52 pm |
|
|
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
|
|
Posted: Sat Mar 29, 2008 1:59 pm |
|
|
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
|
|
Posted: Sat Mar 29, 2008 9:39 pm |
|
|
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
|
|
Posted: Mon Mar 31, 2008 7:53 am |
|
|
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
|
|
Posted: Mon Mar 31, 2008 9:06 am |
|
|
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).
Humberto |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Mon Mar 31, 2008 3:03 pm |
|
|
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
|
|
Posted: Mon Mar 31, 2008 3:39 pm |
|
|
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
|
|
Posted: Mon Mar 31, 2008 4:11 pm |
|
|
Gotcha! |
|
|
|