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?

Daniel

6
Contributors
16
Replies
17
Views
8 Years
Discussion Span
Last Post by Menster
``````\$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.

Depending on what input Coleguitajuan?

Airshow

Menster, that is what needed, thank you! you rock!

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 ?

Can you guyshave a solution without using the rand function??

thanks you!

Can you guyshave a solution without using the rand function??

thanks you!

Do you mean this?

``````\$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';
}``````

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.

If you don't want a repeated result then the following code should do the job.

``````<?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();
?>``````

Great Code , Its great for all

Thank you very much!
How can I save checkid() into a variable?

I tried sales_code = echo checkid(); but no luck.

I simply don't get it! Am I being thick?

Why should you care whether the random number is repeated or not when the function checkId() only returns \$sales_code of which there are only four possible values?

If checkId() returns "VW", why do you care whether it resulted from rand() == 10 or rand() == 15 or rand() == 37 or rand() == 62 or whatever? The nett effect is identical.

Moreover, by "throttling" the random process in this way you are distorting the probabilities you think you are getting. Before it runs out of integers, the process will tend to eqi-probability in all categories!!!

If this is what you want then fine, but please be aware of what is actually going on here.

Airshow

What the heck are you guys doing? He obviously posted a homework question and he never showed any of his own code in this entire thread, all he said is "thanks for the code" and you just handed him answers.

Sorry but my php skills are not good as a professional, and that script was making me crazy. Of course I was testing with my own code but I didnt published it because was completly unservible for you.

Oh dear, I feel bad now. Maybe I'm guilty of souring the tone of a friendly little topic by questioning things too much.

Good luck with it Coleguitajuan. I'm sure cwarn23 has led you in the right direction.

Airshow

Yes, that was very usefull for me. Thank you

Here, I use an array to prevent values from recurring after they have occured once.

``````\$arr = array();
\$values = array();
while (count(\$values) < 100)
{

\$value = intval(rand(1, 100));
if (!in_array(\$value,\$arr)
{
array_push(\$arr,\$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';
}
array_push(\$values,\$sales_code);
} else {
break;
}
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.