Will not increment according to modulus - c#

I'm trying to increment the number of headers based on the page click by the user. But it seems I cannot get the proper results. (The number of headers should also increment.)
I just want to have 1 header only on page 2, 2 headers in page 3, and so on.
Trying to get a result like this :
Clicked page : 1 2 3 4 5 6 7 8 9 10 11
no of headers: 1 1 2 3 4 5 5 6 7 8 9
Here is my code :
var page = 3; //example only
var api = page;
if (page % 5 != 2)
{
api -= 1;
}
else
{
api++;
}
What am I doing wrong?

Related

Why Random range give same output value every time? [duplicate]

This question already has answers here:
Random.Next returns always the same values [duplicate]
(4 answers)
Closed 8 months ago.
Here is the code which i used to for implementing Random class
class Program
{
public static void Main()
{
for (int j = 0; j < 5; j++)
{
foreach (var item in GenerateRandomList(new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }))
Console.Write(item + " ");
Console.WriteLine(" ");
}
}
public static List<int> GenerateRandomList(List<int> arr)
{
var random = new Random(new Random().Next(1,10000));
var ls = new List<int>();
while(arr.Count>0)
{
var index = random.Next(0, arr.Count-1);
ls.Add(arr[index]);
arr.RemoveAt(index);
}
return ls;
}
}
below are the results I am getting
first time
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0
4 3 8 2 1 6 7 5 9 0
second time
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0
6 3 4 2 8 5 9 1 7 0
third time
9 2 4 8 1 6 3 5 7 0
9 2 4 8 1 6 3 5 7 0
9 2 4 8 1 6 3 5 7 0
9 2 4 8 1 6 3 5 7 0
7 1 3 6 5 2 8 9 4 0
and So on..
what am i Missing?
sometimes it give different result but again it starts to repeat previous result.
new Random() creates a new random number generator instance, with the default seed.
In .NET Framework, the default seed value is time-dependent. In .NET Core, the default seed value is produced by the thread-static, pseudo-random number generator. From docs:
If the same seed is used for separate Random objects, they will generate the same series of random numbers.
If the default seed is time-based, and you create five Random objects in a quick succession, each of your generators will get the same seed (since time is not as precise that each instruction executes on a different timestamp), and thus produce the same pseudorandom number sequence.
You will want to create a Random instance only once; for example, as a class field.

Lookup from a 3-column table and interpolate one of the columns?

I have a table with 3 columns: Temperature, Pressure, and Gain.
interface IEntry
{
public float Temperature {get;}
public int Pressure {get;}
public float Gain {get;}
}
Given a certain "input" pair (Temperature_input, Pressure_input), I would like to match this pair with the ones in the table and lookup the corresponding gains, interpolating in certain cases.
Example:
T P G
3 1 0
3 2 1
3 3 2
3 5 4
6 1 0
6 2 2
6 3 3
8 1 0
8 2 3
9 1 0
Input pair (T = 4, P = 2):
Step 1: As the exact temperature T = 4 is not in the table, the immediate lower and upper temperatures 3 and 6 are examined (8 table entries)
Step 2: The pairs for T = 3, T = 6 are sorted by Pressure (which is an int)
3 1 0
6 1 0
3 2 1
6 2 2
3 3 2
6 3 3
3 5 4
3 5 4
Step 3: Linear interpolation between temperatures of the same pressure:
3 2 1
6 2 2
Result is the interpolated Gain = 1 + (2-1)/(6-3) = 1.33
What is an efficient way of implementing the lookup part of the table, that will be accessed millions of times?

C#'s Random producing recognisable pattern when using sequential seeds

Given the following:
for(int seed = 0; seed < 100; seed++) {
var random = new Random(seed);
var roll = (random.Next() % 6) + 1;
Console.Write(roll + " ");
}
I'm seeing a clear pattern in the output:
1 1 2 2 3 3 4 4 5 5 6 6 1 1 1 2 2 3 3 4 4 5 5 6 6
1 1 2 2 3 3 4 4 5 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6
1 1 2 2 3 3 4 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6
1 1 2 2 3 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 5 5 6 6
I do understand that recreating a new Random object every time with a sequential seed isn't the "proper" way of using the class, and that strictly speaking I should be keep the Random instance around, calling Next() on it each time.
However, my use case is that I need to be able to exit my program and restore the state at any moment, and continue the sequence of numbers.
I also need the user to be able to set a seed manually, so I was hoping to be able to do something like:
var random = new Random(userSeed + nextValueIndex);
var chosenValue = random.Next();
I've also tried random.Next(min, max), and it gives different results, but also in a clear pattern.
Clearly my approach isn't the right way to use the Random class, but what is, for my use case?
I understand that random number generation is a very complex topic, so while I'm sort of interested in an explanation of why this is happening, I'm most interested in a practical (easy?) solution ;-)
Here's my solution, which seems to work nicely. Rather than seeding Random with userSeed + index (where index counts from zero), I simply cache the exact result from Random as previousRandom, and then seed with userSeed + previousRandom. That way, I get nice big numbers as the seed, and it avoids the sequential pattern. (previousRandom is initialised as zero, but that's not a problem.)
So, this is roughly what my code looks like:
var nextSeed = userSeed + this.previousRandom;
var randomObj = new Random(nextSeed);
var nextRandomValue = randomObj.Next ();
var chosenValue = (nextRandomValue % randomRange) + rangeMin;
this.previousRandom = nextRandomValue;
return chosenValue;
The one remaining thing I should probably do is make sure that userSeed + previousRandom doesn't overflow (or overflows predictably), because previousRandom might get very close to int's upper bound (up to whatever range I allow for userSeed).

permutations of k objects from a set of n algorithm

I'm looking for an efficient way to achieve this:
you have a set of numbers, let's say that our set is equal to 4 (N = 4);
you have to generate all permutations of 3 elements (K = 3);
Output for N = 4 and K = 3:
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
Anyone have a great, nice'n'quick algorithm up their sleeve or web reference??
Thanks!
Something like this pseudocode:
permute(set, output, len) //output will hold all the permutations
for each number in the set do
choose number and store it at output[0]
if(!empty(set))
call permute(set{without the number}, output + (len - 1)!, len-1) //adjust the position
Invoke by permute(set, output, k)

Paging through an IEnumerable

I have an IEnumerable object (IEnumerable<Class>) and I would like to retrieve a specified line from the object. So if I'm on page two I would like to select row two from the IEnumerable object and then pass it on to another class etc.
I'm a bit stuck at the moment, any ideas?
Look at the functions .Take() and .Skip(). I normally do something like this:
IEnumerable<object> GetPage(IEnumerable<object> input, int page, int pagesize)
{
return input.Skip(page*pagesize).Take(pagesize);
}
If I understand your requirements correctly, something like this paging mechanism should work:
int pageSize = 10;
int pageCount = 2;
iEnumerable.Skip(pageSize*pageCount).Take(pageSize);
This example shows 10 rows per page and a page number of 2. So, it will skip to page 2 and take the first row on that page.
Assuming that pages and rows start at 1, and there is a fixed number of rows per page (say 10), you need to transform the page number and the row to an index as follows:
Page 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 ...
Row 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 ...
↓
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
Code:
int page = 2;
int row = 2;
int rowsPerPage = 10;
IEnumerable<MyClass> source = ...
MyClass result = source.ElementAt((page - 1) * rowsPerPage + (row - 1));
So to get row 2 on page 2, you need to skip the first page (10 elements) and then take the second element (index 1 in that page).
I've implemented a dynamic solution in vb.net, i hope helpful:
<Runtime.CompilerServices.Extension()>
Public Function Paginate(Of T As {Class})(source As T, skip As Integer, take As Integer) As T
If source IsNot Nothing AndAlso TypeOf source Is IEnumerable Then
Dim chunk = (From c In DirectCast(source, IEnumerable)).Skip(skip).Take(take).ToList
If chunk.Count = 0 Then Return Nothing
Return AutoMapper.Mapper.Map(chunk, GetType(T), GetType(T))
End If
Return source
End Function

Categories

Resources