How do I generate a positive long integer in C#? [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 5 years ago.
Improve this question
How do I generate a positive long integer in C#?
Background information: I have no shared state, and am inserting entries into dynamodb, which has no auto-incrementing primary keys. A typical pattern is to insert a guid, check for collisions; depending on how occupied the table is, this is a working solution. I would like to use a modified strategy, where instead of using guids (16 bytes) I will use positive longs (63 bits).
Justification that this is a safe plan:
I expect to eventually use at most a hundred billionth (1/10**8) of the total available space.
2**63 == 9223372036854775808
if I store 100b records, occupancy is:
2**63/10**11 == 92233720 ~= 10**8
Which means the chance of collision is about one every hundred million tries. Since I detect collisions upon insert, that it is acceptable to have to retry the generation at that frequency.
So the question is:
How can I generate random positive longs in C#?

The question asks for how to generate positive longs in C#. This code does that.
The question outlines a plan to handle duplicates. Therefore the following code does not generate PKs, or claim uniqueness - it claims well-distribution and is relying on the outlined framework for handling longs.
So, here is how to generate positive longs in C#, which is what the question asked for:
public static long GeneratePositiveLong(Random random)
{
byte[] buf = new byte[8];
random.NextBytes(buf);
//the last byte must be from 0-127
buf[7] = buf[7] &= 0x7f;
long res = BitConverter.ToInt64(buf, 0);
return res;
}

One possibility is to use a table to keep track of the next PK to use for your other table(s). Lock this table when you need a new primary key, read the current value, increment it, then unlock. This way multiple clients will not overlap keys.

One possible solution is you could use currenttimestamp in nanoseconds which would be unique so that you can use it as a unique primary key as well

Related

Is there a NuGet package or Built in method to convert a string into another meaningful string or Dummy data using c# [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 2 years ago.
Improve this question
I want to convert a set of string into another meaningful string using c#
For Example
I want to convert the string "Aneesh" into "Syam"(The Name should Always convert into same Dummy name Always)
The Dummy values should be consistent based on an input seed
The Random Class of C# uses Seeds, if u would for example when you want to convert Aneesh you would put Aneesh as the input seed (just convert it into int with a method you like or set a reference int to it) and the Random Class if im not wrong should always do the same randoms then. Just convert those via int -> char -> string and you got your random word.
another way of doing it obviously would be setting up a consistent pair of strings, and just when converting taking the other one.
But i dont understand what you mean Aneesh into Syam, because u first said you want another meaningfull string which is consistent, and in the other hand you want string a swapped with string b, these are different things and can be acomplished in diff. ways
The simpliest solution that comes to my mind is to add 1 on every character:
var result = input.ToDictionary(x => x, x => new string(x.Select(y => y + 1).ToArray());
This produces different values for different keys, but is highly deterministic and unabmigious.
[
["Aneesh", "Boffti"],
["Head", "Ifbe"],
["Hand", "Iboe"]
]
Maybe you also need some check that character do not "overflow", that is z + 1 results in {, but instead of A.
If you need a bit more randomness you can of course add any different number than 1 and add that to every character.
Another appraoch which is not de-anonymizable as it produces compeleteley unrelated data is to just randomize based on the index within your input-list:
var result = input.Select((x, i) => $"Dummy{ i }");

Choosing users for A/B testing [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 7 years ago.
Improve this question
I have a table with userid's (int increment) about 4M records.
I would like to show a new feature to only 25% of the users.
What would be the best way to choose those 25% users?
you could use some sort of Fisher Yates shuffle algorithm to randomly choose the 'first' 25% of user ids.
ie, pick a random number between 1 and 4M. that's your first user, add it to a collection somewhere. repeat until you have 1M (25%) users in your collection.
Once you have all the users, mark them somehow in your table.
Another idea is that you could set a browser cookie which was 'myAwesomeFeature=A' or 'myAwesomeFeature=B'. If the cookie isn't set yet, set it to 'B' if a random number between 0-100 is 25 or less. 'A' otherwise. If the cookie is already set, just use it.
Has the added benefit of being easily testable since you can easily force yourself to be in whichever group you want. And it'll work for multiple features since you just need to change the cookie name. And the same user will get the same group as long as the cookie doesn't expire.
How about this?
if (user.Id <= (maxId * 0.25))
{
// Show a new feature
}

Do upper case letters take up more memory? [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 7 years ago.
Improve this question
I'm trying to create a file system that will handle lots of searching through the directories. Would it make a difference if I used upper or lower case letters in terms of memory usage on the folder names?
Case does not affect the size of a character. Some characters take up different sizes in certain character encodings, but generally letters from the same language all have the same size.
No. Each character takes up the same amount of memory.
You can get into some technicalities with character sets and encoding, but unless you've got a really obscure one, uppercase and lowercase use the same number of bits.
No. Especially assuming that you're only using ascii characters.
No. Both are of type char which is defined in C# as 16-bit long numeric value. More reference:
https://msdn.microsoft.com/en-us/library/x9h8tsay.aspx
A data type char must have at least big enough to contain an encoding of at least the 95 different characters which make up the basic execution character set.
This equals a minimum of 8 bits, or one byte. Meaning a or A in a variable char will at least require 1 byte. So no, it's the same.

Adding two different digit Numbers in c# ( without using BigInteger) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a Task to do C#. I need to add two numbers.
The first number contains around 100 digits like "12822429847264872649624264924626466826446692............"
and second number also with 100 digits or more or less
by using this numbers i need task like add/sub/multiply/div
I done this using BigInteger in C#
But do I need to do this using arrays or strings?
Since they are both 100 digits just start with the last digit and in a for loop just add each one, but if the value is > 10 then remember to add one to the next digit.
This is how children learn to add, you just need to follow the same steps, but the answer should be in an array of 101 characters.
UPDATE:
Since you have shown some code now, it helps.
First, don't duplicate the code based on if str1 or str2 is larger, but make a function with that logic and pass in the larger one as the first parameter.
Determine the largest size and make certain the smaller value is also the same size, to make math easier.
The smaller one should have leading zeroes (padding), again to help keep the code simple.
You can also start by looking at the source code for structures such as BigInteger. They would provide you more insight into aspects such as computational efficiency and storage, particularly about multiplication and division. You can take a look at here or here.

Fastest way to search for terms in a text file? [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 8 years ago.
Improve this question
I have a list of terms (words), say around 500,000, they are loaded into some data structure, like a Dictionary or Trie perhaps.
In my program I want to open each text document and search for occurrences of these terms. When i find one I want to stop and transform the string in the text file (replacing it with the transformed string), and continue searching. Once complete with the file, I write to disk the new modified file.
My questions are as follows
What would be the best data structure to use for this purpose - a Tree type structure or .NET Dictionary
How do i search the text? Do I break it up into words and compare each chunk against the list I have, or some other method like RegEx, or .NET methods like Contains()?
I'm just looking for some advice on where to start, because I think speed will be really important when I'm dealing with very large and numerous text files.
EDIT: Yes the Transformation is same for each string - based on an algorithm - so each string will look different though. (like for example using a Cipher on the word to make is unreadable. Anyway I'm just looking for someone to point me in the right direction, I'm not familiar with many algorithms and data structures out there.
From a class I took once, I remember we covered a couple of different algorithms. Here are the ones that I remembered to be pretty effective with large text files...
Boyer-Moore:
http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm
Knuth-Morris-Pratt:
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
These will only help with the lookup, then you can do the manipulation yourself
A hash table (Dictionary) is going to give you faster lookups than a tree structure. A well-built hash table can find a matching word entry with two or three probes, while a tree structure may require up to an order of magnitude more comparisons.
As for splitting up the words, it would seem to be simple enough to collect all alphabetical characters (and possibly digit characters) up to the next whitespace or punctuation character for each word. You will probably want to convert each word into all-lowercase before looking it up in the dictionary.

Categories

Resources