mapping numbers on a scale [closed] - c#

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 3 years ago.
Improve this question
I have an array of numbers up to 20000 and I'm trying to assign a weight to these numbers:
The closer a number is to 0 the higher should the weight be. My problem is that I'm trying to make it such that the higher the number is, the smaller should the difference in weight be, for example the weight difference between 1-100 might be 1.5 but the difference between 100-10000 might be 0.5.
I think it's a logarithmic scale, isn't it? I'm not great at math at all.. this is not a homework question, school was out long ago just a hobby question.
What I've tried is that I've mapped weights to my number array by doing a square root on 25000-value but this isn't what I'm looking for. I just put that in so I could see a gradient of weights coming back plus the numbers are just to big, ideally I want the weights between 0.01 and 3.
I don't have any code to show, any help would be appreciated.

While your question isn't really a C# question, I may have an answer for you.
To scale a value with logarithmic spacing, you can use the following formula:
You said you maximum value is 20000 and you want to scale the values from 0.01 to a maximum of 3, so we need to insert the max and scale our formula:
// edit: also the values should be reversed, so subtract the log from 1:
This gives the following values f(x) for values of x:
f(0) = 3
f(1) = 2.79
f(10) = 2.27
f(100) = 1.60
f(1000) = 0.91
f(10000) = 0.21
f(20000) = 0
Would that suffice for your case?

Related

C#/Unity - How to get only Distinct values (with threshold) and a specific count? [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 have the following Problem:
I have a list Vector3 positions of an object I tracked in Unity (e.g. an animated sphere flying some track). I am adding the position of it every frame to the List. Which leads to a sum of e.g. 500 values. When it's animation stopped i want to "clean" the recorded track and only keep the most distinct values (in the right order). But the List.Count has to be EXACTLY 100. So it has to check for "equal" values with a threshold.
What i've done so far: I am recording the position of "trackableObject" as Vector3 in every frame and cleaning it directly (only keep values that are further away than 'cleaningDiffTolerance' and/or have a greater angle difference then 'cleaningAngleMaxDiff'). This way i am getting only values for significant changes in direction/distance and get more points i curves/corners.
What i want: Do the cleaning not every frame but do it after i stopped recording. Then I want to only keep the most distinct values in correct order and exactly 100 values.
It depends how precise your result needs to be (and how you define 'precise').
The first question would be:
Must the 100 values be exact position values from the first list, or is it ok if it is near.
Position values won't change much every frame. An easy way tou solve the problem would be to average every step:
Compute how many values must be grouped together: n = totalValues/100
Take the first n values and store the average in your final list
Do the same for the next n values, and so on
Alternatively, if you need to have exact values, replace step 2 by "take first value of the groupe" for example.
This approche will be precise enough if the move is smooth.
The problem with this is that if you have a sudden position change (like an angle instead of a smooth turn), you will likely not get the exact position at which the angle occure. The only solution to identify these is to do some more advance analysis. You can search on google for "High Pass Filter" for instance.
I would recommend trying the simple approach first and see if it is fine for your needs

What relevance is 1.307 to the series 1 + 1/2 + 1/3 + 1/4... + 1/n [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm currently working my way through the Nakov book, Fundamentals of Computer Programming in C#. In Chapter 4 question 12 states:
Write a program that calculates the sum (with the precision of 0.001) of the following sequence: 1 + 1/2 - 1/3 + 1/4 - 1/5 + …
It seemed to me to be a relatively straightforward question. The series is a diminishing fraction that does not have an asymptote. Stopping the loop at a certain point due to diminished changes in value meets the precision requirements AFAIC. However, the solution given in both the Hungarian and English versions of the book makes reference to an obscure (to me) value of 1.307. As follows:
Accumulate the sum of the sequence in a variable inside a while-loop (see the chapter "Loops"). At each step compare the old sum with the new sum. If the difference between the two sums Math.Abs(current_sum – old_sum) is less than the required precision (0.001), the calculation should finish because the difference is constantly decreasing and the precision is constantly increasing at each step of the loop. The expected result is 1.307.
Can someone explain what this might mean?
Note that header contains "harmonic sequence" that has no limit.
But question body shows alternate sign sequence that converges towards value 2 - ln(2)
The expected result is 1.307.
I think they are simply saying what the result of the calculation is, so you can check your answer.
The sequence you've got
1 + 1/2 - 1/3 + 1/4 + ...
is the same as the Alternating Harmonic Series on Wikipedia, except with the signs from 1/2 onwards flipped:
1 - 1/2 + 1/3 - 1/4 + ... = ln 2
and the natural logarithm of 2, ln 2, = 0.693. Hence your 1.307 here = 2 - ln 2.

Generating a nice amount of range elements between two numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm trying to figure out a good way to write a little "algorithm", which would be able to find a mathematical a range between these two numbers:
Let's suppose maximum number is 1500 and minimum number would be 1;
By performing some sort of mathematical formula, method would be able to determine that best range between these two numbers is lets say 100;
So range would be:
100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500
Other example:
Maximum is 10, minimum 1;
Best range would be (let's say):
2,4,6,8,10
Are there any libraries in c# which offer this kind of solution or is there some neat mathematical formula used to determine this?
P.S. Guys there can be a remainder in the number as well...
I'm guessing I can divide the maximum number into let's say 7 fixed groups, and then just add up the divided number until I get the max value , no ?
Okay guys I've figured out an idea, lets suppose maximum number is a floating point number and is: 1326.44..., while the minimum is 132.5
I'm going to say that maximum range can be 7... So what I can do is divide 1326.44 with 7 and I'll get 189.49
So the first amount in range is:
var ranges = new[] { 132.5, 189.5 ... /*Now I just need to dynamically somehow add the rest of the range elements?*/ };
This is actually super easy. You have a min range value and a max range value, and you want a particular number of items in your range. Therefore, you simply need to calculate a step value, and then add that recursively to the minimum value until you're at the maximum value. For example:
var min = 132.5;
var max = 1326.44;
var count = 7;
var step = (max - min) / count;
var items = new List<double>();
for (var i = min; i <= max; i += step)
{
items.Add(i);
}

How to generate a bell curve (Gaussian function) [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 9 years ago.
Improve this question
I'd like to be able to generate a bell curve to use for probability in procedural generation in games.
For, example if I want to generate a forest, I could give it a radius and intensity and make a bell curve of the right shape to give me the probability of whether a tree should be placed or not. I should end up with a lot of trees in the centre and they would become less frequent as you approach the radius distance out from the centre.
I've done something similar before using a sine wave. But I have no idea how to make a bell curve. These equations are greek to me. I have forgotten how to read them, but it would be greek to the computer anyway.
Could someone write down the equation for bell curve in C# (or python would be my 2nd choice) and maybe explain it a bit?
Sure.
p(x) = exp(-(x-mu)^2/(2*sigma^2))/sqrt(2*pi*sigma^2)
The bell curve is also called the gaussian probability distribution. It is basically taking e to the power of a negative square of the x value. The rest is to make sure that it is centered at mu and scaled to the specifics of the particular problem you are modeling, and to make sure that the integral over all values of x sums to 1.
To generate random samples from this distribution in Python, you can do something like the following:
import random
sample = random.gauss(mu, sigma)
# where mu is the center of the bell curve, and sigma is proportional to its "width"
If you want to have a two-dimensional bell curve, it's nice to know that you can find X and Y values separately like in the above, and the 2D plot will be a 2D bell curve, where the density is highest at the center (mu_x, mu_y).

For an RPG, scaled integer vs decimal? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Yes, I realize decimal or float has been asked before, and I know their uses and that decimal is precise and float is non-accurate.
In a game I'm working on, I have the player, which has a movement speed of 36 pixel movement per second. I update this about 10 times a second, making it 3.6 each time. I want this to be precise, but decimal takes up 16 bytes (128 bits), which is a lot. If I want to keep the accuracy, should I use integer instead and scale the numbers? Or.. Is float more suitable? Float and Int32 both take up 4 bytes, but an integer doesn't loose any numbers. So shouldn't Integer be more suitable? Then why would anyone ever use float?
So basically, which one should I use for speed and accuracy? I'm using decimal at the moment. I want to change it before I get into too much detail in the game, or it's going to get harder to make all those changes.
16 Bytes is a lot compared to other types. Go with what satisfies your requirement, no premature optimization.
You might also consider using int and interpreting it as x/10, that way the inaccuracy won't accumulate. But only if those 128bits really bug you (which they shouldn't, imho).

Categories

Resources