Generate a value depending on algorith

Reply

Join Date: Jun 2009
Posts: 12
Reputation: coleguitajuan is an unknown quantity at this point 
Solved Threads: 0
coleguitajuan coleguitajuan is offline Offline
Newbie Poster

Generate a value depending on algorith

 
0
  #1
Jun 29th, 2009
Hi all!

Im trying to creat thecode for save a value into a variable ($sales_code) formed per 2 letters (VW, DF, RK or DI) depending on this algorithm: 79% of the time it's VW, 7% of the time it's DF, 7% of the time it's RK, 7% of the time it's DI.

could you give me a hand on this?

Thanks in advance!

Daniel
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 171
Reputation: Menster is an unknown quantity at this point 
Solved Threads: 22
Menster's Avatar
Menster Menster is offline Offline
Junior Poster

Re: Generate a value depending on algorith

 
0
  #2
Jun 29th, 2009
$value = intval(rand(1, 100));
if ($value < 80)
{
$sales_code = 'VW';
} else if (($value > 79) && ($value <= 86)) {
$sales_code = 'DF';
} else if (($value > 86) && ($value <= 93)) {
$sales_code = 'RK';
} else if (($value > 93) && ($value <= 100)) {
$sales_code = 'DI';
}

Hi there, hope this code snippet helps. Based on a random int, so it's gonna be random, but by the law of averages it'll work for what you need.
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 821
Reputation: Airshow is on a distinguished road 
Solved Threads: 116
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: Generate a value depending on algorith

 
0
  #3
Jun 29th, 2009
Depending on what input Coleguitajuan?

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: coleguitajuan is an unknown quantity at this point 
Solved Threads: 0
coleguitajuan coleguitajuan is offline Offline
Newbie Poster

Re: Generate a value depending on algorith

 
0
  #4
Jun 29th, 2009
Menster, that is what needed, thank you! you rock!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: coleguitajuan is an unknown quantity at this point 
Solved Threads: 0
coleguitajuan coleguitajuan is offline Offline
Newbie Poster

Re: Generate a value depending on algorith

 
0
  #5
Jul 1st, 2009
Hi Menster,

Rand does not guarantee not to repeat a value twice during a 100 number sequence, so there is any other logic instead of using Rand ?

Thanks for your support
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: coleguitajuan is an unknown quantity at this point 
Solved Threads: 0
coleguitajuan coleguitajuan is offline Offline
Newbie Poster

Re: Generate a value depending on algorith

 
0
  #6
Jul 2nd, 2009
Can you guyshave a solution without using the rand function??

thanks you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,456
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Generate a value depending on algorith

 
0
  #7
Jul 2nd, 2009
Originally Posted by coleguitajuan View Post
Can you guyshave a solution without using the rand function??

thanks you!
Do you mean this?
  1. $value = 50; //input value
  2. if ($value < 80)
  3. {
  4. $sales_code = 'VW';
  5. } else if (($value > 79) && ($value <= 86)) {
  6. $sales_code = 'DF';
  7. } else if (($value > 86) && ($value <= 93)) {
  8. $sales_code = 'RK';
  9. } else if (($value > 93) && ($value <= 100)) {
  10. $sales_code = 'DI';
  11. }
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: coleguitajuan is an unknown quantity at this point 
Solved Threads: 0
coleguitajuan coleguitajuan is offline Offline
Newbie Poster

Re: Generate a value depending on algorith

 
0
  #8
Jul 2nd, 2009
No, the input must be a random, but the values from 1 to 100 cannot be repeated.

I am more thinking along the lines below, because rand is well too random:

1.) Get a count of all records for each sales_code. Lets say we call them $VW_total , $DF_total , $RK_total , $DI_total
2.) Get a total count for all sales_code + 1 (the +1 is for the new record that is going to be inserted). Lets call this total "$grand_total"

Let's see if this clarifies you.

Thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,456
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Generate a value depending on algorith

 
0
  #9
Jul 2nd, 2009
If you don't want a repeated result then the following code should do the job.
  1. <?php
  2. $code_records=array('');
  3. //above line is only ever called once
  4.  
  5. function checkid() {
  6. global $code_records;
  7. $value = intval(rand(1, 100));
  8. $sales_code='';
  9. while (in_array($sales_code,$code_records)) {
  10. $value = intval(rand(1, 100));
  11. if ($value < 80)
  12. {
  13. $sales_code = 'VW';
  14. } else if (($value > 79) && ($value <= 86)) {
  15. $sales_code = 'DF';
  16. } else if (($value > 86) && ($value <= 93)) {
  17. $sales_code = 'RK';
  18. } else if (($value > 93) && ($value <= 100)) {
  19. $sales_code = 'DI';
  20. }
  21. if (!in_array($sales_code,$code_records)) {
  22. $code_records[]=$sales_code;
  23. break;
  24. }
  25. }
  26. return $sales_code.'<br>';
  27. }
  28.  
  29. echo checkid();
  30. echo checkid();
  31. echo checkid();
  32. ?>
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 197
Reputation: ayesha789 is an unknown quantity at this point 
Solved Threads: 3
ayesha789's Avatar
ayesha789 ayesha789 is offline Offline
Junior Poster

Re: Generate a value depending on algorith

 
0
  #10
Jul 2nd, 2009
Great Code , Its great for all
Ayesha
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC