| | |
Generate a value depending on algorith
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2009
Posts: 12
Reputation:
Solved Threads: 0
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
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
$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(); }
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
•
•
•
•
Can you guyshave a solution without using the rand function??
thanks you!
php Syntax (Toggle Plain Text)
$value = 50; //input value 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'; }
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
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
•
•
Join Date: Jun 2009
Posts: 12
Reputation:
Solved Threads: 0
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!
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!
If you don't want a repeated result then the following code should do the job.
php Syntax (Toggle Plain Text)
<?php $code_records=array(''); //above line is only ever called once function checkid() { global $code_records; $value = intval(rand(1, 100)); $sales_code=''; while (in_array($sales_code,$code_records)) { $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'; } if (!in_array($sales_code,$code_records)) { $code_records[]=$sales_code; break; } } return $sales_code.'<br>'; } echo checkid(); echo checkid(); echo checkid(); ?>
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
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
![]() |
Similar Threads
- Generate Thumbnail images on the fly. (PHP)
- Automatically Generate PDF Files in DWGeditor (Visual Basic 4 / 5 / 6)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
- Need to know how to create a database that will generate a random number (Database Design)
- special keys as inputs (Game Development)
Other Threads in the PHP Forum
- Previous Thread: WAMPServer 2.0 on Vista does not run PHP
- Next Thread: Destroy session on clicking the Back button of broswer
| Thread Tools | Search this Thread |
apache api array beginner binary body broken cakephp checkbox class cms code computing cron curl database date date/time delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime global google host href htaccess html image include insert ip javascript joomla limit link list login mail memmory memory menu mlm msqli_multi_query multiple mycodeisbad mysql navigation oop parameter parsing paypal pdf php problem query radio random recourse recursion regex remote script search seo server sessions sms snippet source space sql static syntax system table thesishelp tutorial update upload url validator variable video web webdesign wordpress xml youtube






