I'm sure this is possible but my math isn't that fantastic.
I'm showing latest movies on my page and my application uses a 5-star rating system, however, the data I receive from a Web Service arrives as a percentage e.g. 50%.
Is there any way I can convert this percentage to a star rating equivalent, which in this case would be 50% = 2.5, allowing me to show 2.5 stars?
It seems fairly simple when I have 50% but if I get 94%, it confuses my poor little pea for a brain! Please help.
If you want to convert the 0..100 scale to a 0..5 scale, just divide by 20.
If you want it on a half-star boundary, then divide it by 10 instead and that's the number of half-stars you need.
Keep in mind I'm talking about integer division here, where the value is truncated (rounded down).
You may also want to consider rounding it more intelligently during the division, rather than truncating, so that something like 99% is 5 stars (not 4.5). This can be done by simply adding half the amount you're dividing by before the division, something like (in C):
int percent = 94;
int halfstars = (percent + 5) / 10;
This would give the following results for input values between 0 and 100 inclusive:
percent halfstars
------- ---------
0- 4 0
5- 14 1
15- 24 2
25- 34 3
35- 44 4
45- 54 5
55- 64 6
65- 74 7
75- 84 8
85- 94 9
95-100 10
The formula for finding the percentage of a number is fairly simple:
$percentInDecimalForm * $number
For example, a 94% rating would be:
.94 * 5 = 4.7
You just need to solve the following:
100% ---------- 5
94% ---------- x = (94 * 5) / 100 (=) x = 4.7
Now it's necessary to know the granularity of your star scale (how many times you can divide the star).
Since you mentioned 0.5 stars, I'm gonna assume your star granularity is 1 / 0.5 = 2, so just solve:
round(4.7 * 2) / 2 (=) 9 / 2 (=) 4.5
Related
I have a list of questions in a category, and want to choose a subset of them to ask the user based on which ones they answered right/wrong previously.
I want to make it random, but in a way that the ones they have more trouble with are asked more frequently.
EDIT: I'm trying to figure out how to calculate the weight/bias/score for each question based on the number of times they've answered it right/wrong.
I came up with the following, but it seems odd to me:
I assign a score to each question based on how many times they answered it right/wrong
Obviously, if they've never been asked that question I need to assign an arbitrary score (I chose 5)
For all other question, I use the formula
score = wrong*2-right
so if I had the following 10 questions, the "score" would be calculated for each of them (R=# of times they got it right, W=# of times they got it wrong and S=score). From there, I take the lowest score and assign that a probability of 1 (in this case it was id=5 with a score of -7). I then take the difference between the lowest score and the second lowest score (id=1 with -5, a difference of 2) and assign it a probability of 1 + the difference = 3.
I continue this for every question, and then at the end I can just choose a random number between Min(1) and Max(82) and select the question that has the highest P where random < P. So if my random # was 79 I would choose id=2.
But this seems long and convoluted. Is there an easier way to do this (I'm using PHP and mysql, But I plan to do this within an app with a local datastore as well)
id R W S P
1 5 0 -5 3
2 3 5 7 82
3 6 2 -2 8
4 2 2 2 23
5 9 1 -7 1
6 3 1 -1 14
7 0 0 5 68
8 7 5 3 33
9 6 5 4 44
10 3 4 5 56
EDIT: to clarify, I'm stuck on the issue of "weight" (P value in my example)...I'm trying to find a good (and fast) way of calculating the "weight" for each problem, given the number of right and wrong answers they've given for the question
I am not sure if I understand your answer correctly but it seems you are looking for a sort of "weighted" random number generator. In essense what you want to do is give the problems they are having issues with more weight. Perhaps create a class called questions with a property of weight in it. That property can hold how much weight you put in it. Then when you select a random number generator use something like this.
http://codetheory.in/weighted-biased-random-number-generation-with-javascript-based-on-probability/
After doing some research, I realize that my initial method of calculating a weight is bit slow. After using the formula, I end up with some -ve weights. I then have to go through each one and add ABS(MIN(S)) to each weight, which is unnecessary.
My new formula would be S = CEILING(Wrong * 5 / Right)
Obviously I'd need to account for 0 values, so the code would be:
if (R == 0 AND W == 0) S = 10
else if (R == 0) S = W*5
else if (W == 0) S = CEILING(5/R)
else S = CEILING(W * 5 / R)
I've worked out the numbers for a few sample sets and this gives me fairly good results. It also allows me to keep the SCORE value updated in the database, so it doesn't need to be recalculated every time (just updated whenever that question is answered)
Once I have a set of 60 or so questions and I want to choose 5 or 10 of them, I can just create a random # between 1-SUM(SCORE) and then use a binary search to figure out which question that represents.
If anyone has a better suggestion for calculating the score/weight/bias or whatever it's called, I'd appreciate it.
first , sorry for my english I'll do my best to explain my problem !
So , i'm trying to generate a single elimination tournament with an unlimited number of players.
for now i'm just thinking about it , i have nothing on paper , i think i will not have problem for tournament with power of two ( 2 4 8 16 32 players..) , my brain hangs on players going directly to round 2 , i don't know how to determine this number and where to place them.
eg (with 59 players)
I think there is a formula but I can't find it, I have some ideas but i think too specifically on a case, without knowing if it would work for another .
Thank you if you can help me !
For a given number N, find the difference between it and the smallest power of 2 at least as large as N. For 59, that'll be 5 (64 - 59). Those 5 players will be added to the tournament schedule at the second round.
This algorithm allows for all the players to be part of the game when the second round begins - i.e., as early as possible. Its explanation is very simple: imagine that originally there were 2**N players - but some just didn't come to their games, so their opponents went further without a fight. )
As a sidenote, your formula should take into account that it's strongest players that should enter the game from the second round, not the weakest ones. )
The first step apparently is calculating the number of players that will participate in the first round. Now, let's continue that 'missing players' metaphor - let's say there were 64 players originally, so the first round should have 32 games played. But 5 players (64 - 59) didn't come for those games - so the number of real games is 27 ( 64/2 - 5 ), and the number of real participants of the first round is 54 (27 * 2).
After the first round, there'll be 27 people left in the tournament - those people will be joined by those other 5 guys, so the total number of the 2nd round players is 32. The rest is trivial, I suppose. )
Actually, this is easy to commonize. Let's say we have N players, and the smallest power of 2 at least as large as N is P. Now...
The first round should have (N - (P - N)) (or just (2*N - P)) players.
The total number of the games in the first round is (N - P/2).
Apparently, the same is the number of players going into the 2nd round.
These will be joined by (P - N) players left without a play in the 1st round,
so the total number of players in the 2nd round will be...
N - P/2 + P - N => P - P/2 => P/2
... and from now you just go with the direct schedule of 2^N players (as P/2, as well as P, is the power of 2).
ooh thank you #raina77ow , you blew my mind , So here are my calculations:
64/2 = 32
59/2 = 29 ( rounded to the lower ) => nb of total player at left ( round 1 & 2)
32-29 = 3 => nb players at left going to round 2
29-3 = 26 => nb players at left going to round 1
59-29 = 30 => nb total players at right ( round 1 & 2 )
5-3 = 2 => nb players going to round 2 at right
30-2 = 28 = nb players round 1
`
I think I can make an algorithm now if that's right for each case.
I am trying to create a dice game where you are given a 6 sided dice and you roll but the probabilities of each side is predefined (not fair).
Example:
Side Probability
1 3.63098
2 18.38200
3 10.59424
4 3.87055
5 13.66651
6 49.85572
Total: 100
So Side 6 should roll most often than any other. I've tried the following approach:
3.63098 + 18.38200 + 10.59424 + 3.87055 + 13.66651 + 49.85572 = 100
and generate a random number: rand(0, 100) and pick a number based on where it lied.
However it does not really work with floating numbers, max probability does not have to be
100 it can be 23.994 and will be distributed randomly to 6 sides.
Please suggest an algorithm to use or where to look for algorithms, I'm not asking to write code for me just need to research but dont know what to look for.
You can generate a random number between 0 and RAND_MAX.
Then scale each probability to RAND_MAX by dividing by 100 and multiplying by RAND_MAX. You can get more accurate probabilistic outcomes.
OR
Just divide the generated random number by RAND_MAX and check which range of [0,1] it lies.
First do normalization of probabilty to lie within [0,1]
normalizes prob(p) = value(p)/(sum of all values)
then find range of values of each variable for example
1 => 0.2
2 => 0.3
3 => 0.1
4 => 0.2
5 => 0.1
6 => 0.1
then ranges will be:-
1 =>[0,0.2]
2 =>(0.2,0.5]
3 =>(0.5,0.6]
4 =>(0.6,0.8]
5 =>(0.8,0.9]
6 =>(0.9,1.0]
Then for a dice roll generate a random float in range [0,1]
Find the range in which it is enclosed
eg . random float = 0.37 then side on top will be 2
I have a table called users which currently contains column money of type integer which contains the amount of money the user currently has.
However, I would like to expand and have a couple of more currencies that are based on each other. 1 Gold = 10 Silver = 100 Bronze (or something like that). When you get 100 Bronze, they will convert into 1 Gold. The same with 10 Bronze = 1 Silver. All these currencies have their own column in my table. I have a hook function that is called on every request and updates the money, if possible.
I was told though I could use just one column money and then I can play with variables and take out the different currencies.
But my question is: What would be the best method?
In case the latter method is best: How do I put variables to an already existing object (an object which contains user data; I think it’s called stdClass or something like that)? $user->username, etc.?
Just store the equivalent number of bronze. Then use the following algorithm to get as much as possible converted to gold, from the remainder as much as possible to silver and the rest stays in bronze:
Set the number of gold to zero, silver to zero and bronze to whatever the database says.
Set the number of silver equal to the number of bronze divided by 10 (use integer division). Set the number of bronze equal to the number of bronze mod 10.
Set the number of gold equal to the number of silver divided by 10 (use integer division). Set the number of silver equal to the number of silver mod 10.
So if you have 9,327 bronze:
gold=0, silver=0, bronze=9,327
gold=0, silver=932, bronze=7
gold=93, silver=2, bronze=7
I've done something similar before, and I agree that you should just have the one column for "money", and compute other currencies as-needed. If you had separate columns, you'd have to rely on a trigger or application logic to keep them in-sync.
I would convert everything to bronze and just store that value. Any sort of transactions would also be converted to bronze first. The only time you need to convert to silver or gold would be for display.
One way is to only store all currency in Bronze.
That way if you have 123 Bronze you can convert them
123 / 100 //Gold with integer division which should be 1
Remaining are 123 - NumGold * 100 ie 23
so you have
23 / 10 // Silver with integer division should be 2
Remaining are 23 - NumSilver * 10 ie 3 bronze coins.
This was the SQL knows only about Bronze coins and showing them as Gold , Silver is all in the application layer
The accepted answer doesn’t account for values over 9999 since it will add 10 more to silver and give a false output.
The best solution is this:
var bronze, silver, gold;
var myMoney = 32141; // number from database
bronze = myMoney % 10;
myMoney = (myMoney - bronze) / 10;
silver = myMoney % 10;
gold = (myMoney - silver) / 10;
Output:
gold = 321
silver = 4
bronze = 1
I have a system that monitors the performance of students. It tabulates the number of students who gained a score of 1,1.25,1.5,....5 (this is our grading system). For example:
grading system number of students
1 12
1.25 10
1.5 15
1.75 15
2 20
2.25 1
2.5 5
2.75 6
3 8
5 0
From this example, I need my system to determine which is the mode and then print it. I also need to get the standard deviation.
I need this in PHP. Can anyone help me with this?
Your ideas, comments, and suggestions are appreciated.
Update:
Here's what I've done so far:
Finished the standard deviation...but there are still discrepancies i can't resolve...when i calculate the standard deviation manually..the answer is different from the output of my system.. >.<
While for the mode I used an array..this is my code:
$sample = array($one[$ctr],$two[$ctr],$three[$ctr],$four[$ctr],$five[$ctr],$six[$ctr],$seven[$ctr],$eight[$ctr],$nine[$ctr],$ten[$ctr],$fda[$ctr]);
rsort($sample);
$holder = $sample[0];
//$holder = $mode;
The sorting is successful and I can the highest number but I need to print the value of $holder to a table using fpdf.
Any ideas, why the value is not visible in the output?
Well, the mode is easy. Just find the grade (2) which has the highest number of students (20) and there you are.
If there's more than one, then it's multi-modal and you should probably allow for that.
For the standard deviation, the method can be found here. It's basically working out the mean of all those numbers (let's simplify this by using 1, 1, 2 and 7):
1 + 1 + 2 + 7 10
------------- = -- = 2.5
4 4
then calculating the square root of the variance of all those samples from that mean:
_____________________________________________
/ (1-2.5)^2 + (1-2.5)^2 + (2-2.5)^2 + (7-2.5)^2
/ ---------------------------------------------
\/ 4
__________________________
/ 2.25 + 2.25 + 0.25 + 20.25
= / --------------------------
\/ 4
= 2.5
If you're asking a beginner-level question like how best to do this in a specific language like PHP, you should investigate the use of arrays and loops.