How to Program 'Chance'? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i have an idea of something but i don't have a clue how to do it :D
basically just like in the games for example, every 6 hours you can open a chest where you can grab some item for free. Now i want to have a chest that the user can open every 6 hours, i have the items written in mysql server where every item gives experience points to the user. but how to do it?
lets say the database have 15 items, now every item have its chance value for example
nothing = 75% of getting it
sword_1 = 15% of getting it
sword_2 = 30% of getting it
any idea how to do it?

Define a range of % to get an item
Example - sword_1 = 0-15%, sword_2 = 15-45%
Note that the numbers above don't add up to 100%, so unless you really want return more than one time, sometimes.. the above ranges takes care of getting nothing.
If you want multiple rewards on a roll, simply adjust the ranges accordingly.
Generate a random number between 0 and 100. For the items where generated number falls within the range, those are your rewards won for that round.

You could assign a numeric value to each of your items in the table, the percentage of getting it would be the difference between it and its next lowest number.
Nothing, 60 (60% chance)
sword_1, 70 (10% chance)
sword_2, 100 (30% chance)
Make sure that your max item is 100.
You can then do a select from that table with a random number:
SELECT * FROM ChestItems WHERE ChanceValue < FLOOR(RAND() * 100) ORDER BY ChanceValue DESC LIMIT 1;
There are several (and much better) ways you can do this. This is just a quick example.

Related

How to calculate a logical curve along a set of values? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to take a float value, with an arbitrary minimum and maximum possible value, and convert it to a linear scale, for representation on a bar-shaped indicator. The problem is, I can't just lerp it between the minimum and maximum, because the maximum value will always be dramatically higher than the minimum value. I have an array of arbitrary values that I want to act as intermediate points between the minimum and maximum. Now I just need to calculate a logical best-fit curve through the points. Each value is always larger than the last, and the rate of increase in value accelerates the further up you go, but there is no simple formula for calculating this rate of acceleration.
Here's an example of the values that may be used:
6.0, 13.5, 30.0, 75.0, 375.0
where 6 is the minimum, and 375 is the maximum.
If x is exactly one of these values, I would want a simple value depending on how many total values there are, I.E 0, 0.25, 0.5, 0.75, 1. The issue is calculating the in-between values.
How would I go about achieving this? I apologize if a question like this has already been asked, as it feels like a common problem, although I didn't know what to search for. If this has already been answered before, please just point me in the right direction.
Reposting my comment as an answer, as requested.
If a curve might be y(x) = k^(ax+b), take logs of both sides and you have a linear relation. As pointed out tho, this is maths not programming.
I’d pick k = 2, e or 10 for easier implementation; a & b you work out from data.

How do I take an indefinitely large set of user inputs and save them into an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a program (in school for intro to computer programming(dont worry, this is all extra credit stuff, not asking for cheat answers)) that will take a user decided set of user inputs and save them all to do math (more specifically, finding the median, but I want to figure that out myself) on them. I am fairly sure that I need an array to do this (even my teacher hinted that that is what you needed to do).
My plan is to have a variable x that will decide the amount of separate numbers in the array (will not only be the number in the array, but also the number to check how many times I want to run the loop for asking for numbers), and then to have that many user inputted numbers inputting by the user, and then to be able to take those numbers and find the median of them (I will probably have to check if the number is even or odd first, then sort the numbers (somehow), then find the middlemost number.)
Thanks!
Instead of using array which has fixed size, try using List.
List allows you to add indefinitely many (until it fits into memory) elements.
So, in your for loop you could read user input and add it to the list.
After that you can sort entire list using sort method and return middle element, which is a median.
It could look like that:
int n = //TODO: read number of user inputs
List<int> elems = new List<int> ();
for (int i = 0; i < n; i++) {
int x = // get input
elems.Add(x);
}
Console.WriteLine(elems[elems.Length / 2]);
You can take a look at the List<T> class here for storing the numbers.
For reading input, see Console.ReadLine.
For converting string to int, take a look at int.TryParse.
I gave you the tools, now start coding! :D

Iterate with threshold stored in database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a variable number of transactions in every month saved in the database. I have to calculate a payment according a structure like as 0-100 transaction, 1 € for every transaction, 101-200 2€ for each, 201 to 300 3€ for each, etc. The first 100 transactions I’ll pay 100 €, the next 100 transactions I’ll pay 200 € and in this way. Until here is easy for me, the problem become because the number of thresholds (in the earlier example was 3), can be variable, sometimes is 3, other times could be 2, 4, 5 or whatever. These thresholds are stored in a table in the database. Please, can someone help me. Thanks in advance.
For each threshold in the database, you will sore a LOWER and UPPER bound and Price (0,100,1),(101,200,2),(201,300,3).
Load the thresholds into a List with each member containing the lower, upper, and price
List<int[]> thresholds = new List<int[]>
thresholds.Add(new int[]{0,100,1});
thresholds.Add(new int[]{101,200,2);//etc.
private void calculateTotal(int numberSold){
int total =0;
foreach(int[] bound in thresholds){
if(numberSold>=bound[0]&&numberSold<=bound[1]{
total+= bound[2]*numberSold;
}
}
return total;
}
That's about the best I can do for you based on your limited question. Play with this and if you have a problem, ask a specific question based on the code you create and the specific problem you are having.

Regarding logical equation on my C programming [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a situation on my C programming here and just wondering whether my solution is the correct way:
I have a LED display with particle count sensor and will show 6 digit of seven segment numbers as the count value. The sensor will give voltage input value. The input is from 0V to 10V. So the range of 0V-10V need to be shown in the display as 000000 to 999999 count.
My solution is:
Display number = Input voltage * 99999.9
For example:
Display number = 10.000*99999.9=999999
Display number = 5.500*99999.9=549999
Display number = 2.300*99999.9=229999
Is this the correct solution? I notice that I will get a lot of 9 on the display value.
The most usable and user friendly solution is to ignore the fact that your most significant digit is capable of displaying up to 9 and simply multiply by 10000 unless you desperately need the maxim resolution in which case simply use a scale factor of 100000 and document that your range is 0-9.99999.
My reasoning is that it is better to either loose one digit in the accuracy across the whole range or clip just the maximum value than to have an error across the entire range.

Incrementing across a changeable distance 0-1 keeping time equal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm unable to think through this one I think it's one of those moments where the answer is really simple but I'm too close to the problem to see the solution.
I have a distance that's changeable and an object that has to traverse this distance in the same time regardless of length.
The start of the distance is valued as 0 and the end of the distance is valued as 1.
Obviously the incrementation will be smaller the larger the length to keep the times equal.
What formula could I use to calculate the 0-1 incrementation but keep the time taken equal.
I know it seems overly complicated way to increment but it's part of the third party plugin I've been given.
I'm coding in C#.
Thanks.
[EDIT]
Sorry I wasn't very clear.
For incrementation the start point is always 0 and the end point is always 1.
So the object can move += 0.5 for example.
so when the length increase from say 30 to 65 it should take longer to increment from 0 to 1.
So you are looking for a way to have a number x in the range [0,1] that maps to some y in some arbitrary range [min,max], and are looking for the increment value a such that if x -> y then x + a -> y + b for some constant b? If I have understood your question correctly, then your a value should be:
a = b / (max - min) note: make sure to format this correctly for C#, especially be sure to cast and that sort of thing.
This is basically saying that a should be the fraction of the range that a spans, that if b is half the range from min to max, then a should be 0.5, and if b spans one fifth the range, a should be 0.2.

Categories