How to generate unique number of 12 digits? - c#

I'm working on an app that sends raw data to zebra printer and print out barcodes. And since every item has its own unique barcode, I need to define a variable that automatically generates unique number of 12 digits long.
see example:
printBar prnt = new printBar("123456789012");
Is there anyway to define a double variable and pass it to a function that return uniqely 12 digits number and pass it over again to the printBar class?. But how to make sure everytime you access it returns a unique value?.
I also thought of another way, since am using MS Access db, I can create a column of AutoNumber datatype and assign it to Random, but you don't get the exact 12 digits required, sometimes it generates a value of 10 digits sometimes more or less.

Start with a twelve digit number, ie: 111111111111
to get your new 'random' unique number take the previous number and add 1.
although not random, it will guarantee uniqueness.

How many times do you generate a new barcode per day, hour, minute?
You could use a technique like the auto versioning of Visual Studio works.
Count the number of days from some specific date (e.g. 1.1.2000)
padded with 0 to five places.
Concat the seconds elapsed till midnight
padded also with zero to five places.
Fill up the last two numbers with a static counter in your App that just wrap around at 99.
Example
public static class UniqueId
{
static private int _InternalCounter = 0;
static public string Get()
{
var now = DateTime.Now;
var days = (int)(now - new DateTime(2000, 1, 1)).TotalDays;
var seconds = (int)(now - DateTime.Today).TotalSeconds;
var counter = _InternalCounter++ % 100;
return days.ToString("00000") + seconds.ToString("00000") + counter.ToString("00");
}
With this approach you'll get an overflow at the 15. October 2273, but i think this can be solved by your follower. ;-)
If you need to create more than hundred unique IDs per second you can change the last two line into:
var counter = _InternalCounter++ % 1000;
return days.ToString("0000") + seconds.ToString("00000") + counter.ToString("000");
Now you'll have thousand unique IDs per second, but the days will already overflow at 18. May 2027. If this is too short, you can get additional ten years if you set the start date to 2010 by this line:
var days = (int)(now - new DateTime(2010, 1, 1)).TotalDays;

Using an RNG and a hash do:
10 - stream out 12 digits
20 - check if value is in hash
30 - if it's goto 40 else goto 10
40 - push value into hash
50 - return new 12 digit number
60 - goto 10

Related

C# generate bit string randomly "0001", "1010", etc

I need to generate a 4 bit string randomly and set to a property via Linq.
Right now is hardcoded:
// TODO: hardcode bit string
employees = employees.Select(x => { x.Options = "0101"; return x; }).ToList();
I need Options to be random so I can get all 4 bit possible values: "0001","0010", "0011" and so on.
I was thinking on having a Random 0 to 1 variable and generate the value 4 times and concatenate the string.
Any clue on how to implement this?
If speed is critical, such as this operating being called in a loop, it would be more efficient to use a 16 element string array of all possible values and select the element randomly.
var rand = new Random();
Convert.ToString(rand.Next(16), 2).PadLeft(4, '0')
Explanation:
The first line creates a random object, nothing too hard to understand here... The second line first generates a random number between 0 - 15 (rand.Next(16)). Then it puts the random number into Convert.ToString. The method converts to number to base 2 (because the second parameter is 2). However, this still is not enough because if the random number can be represented by 3 bits or fewer, the returned string will not have the leading 0s. That's why I used PadLeft to add them in.
You can use this
Convert.ToString(int, 2);
This will convert the int to base 2 string.
Where int is a random number up to 16 not included.

Rounding FILETIME in C# to accommodate FAT rounding

I have a Windows FILETIME :
A 64-bit value representing the number of 100-nanosecond intervals
since January 1, 1601 (UTC))
and I need it rounded UP to the nearest even second, as described here.
The code I have so far:
var originalDt = DateTime.FromFileTimeUtc(input);
// round it UP to the nearest Second
var newDt = originalDt.AddMilliseconds(1000 - originalDt.Millisecond);
// then if our new Second isn't even
if (newDt.Second % 2 != 0)
{
// add one second to it, then it'll be even
newDt = newDt.AddSeconds(1);
}
return newDt.ToFileTimeUtc();
doesn't quite work... it turns 130790247821478763 into 130790247820008763, I'm after 130790247800000000.
Maths isn't my strongest subject... can I just zero those last four digits safely? Or should I forget the above code and just zero the last eight digits completely? Or... another way?
Rather than struggling with the DateTime object, you could perhaps more easily just do the raw mathematics:
If input is the number of 100 nanoseconds, then:
/10 for the number of microseconds;
/10,000 for the number of milliseconds;
/10,000,000 for the number of seconds;
/20,000,000 for the number of 'two-seconds';
So:
input = input / 20000000 * 20000000;
The division will round the number DOWN to the last even second, then the multiply will get it back into the right size again.
But you said you wanted it rounded UP:
input = (input / 20000000 + 1) * 20000000;
That adds one 'two-second' to the small number before factoring it up again.
Pedantically, if input was at exactly the two-second mark, then this would add two seconds to it. To fix that:
if (input % 20000000!=0) {
input = (input / 20000000 + 1) * 20000000;
} // if
That checks whether there's any fractional 'two-second' before deciding to bump it up. I'll leave it up to you as to whether you add this extra check...
#Matthew Watson points out that the usual programmers trick for the above problem is to pre-add not quite enough to roll input over to the next 'two-second', then go ahead and do the divide-then-multiply. If input was over the minimum, that'll roll it over:
const long twoSeconds = 20000000;
...
input = (input + twoSeconds - 1) / twoSeconds * twoSeconds;
Work with raw ticks, then round these up to two-second intervals. This is simpler than trying to add or remove things after the comma.
const long twoSecondsInTicks = 20000000; // 20 million
long twoSecondIntervals = originalDt.Ticks / twoSecondsInTicks;
if (originalDt.Ticks % twoSecondsInTicks != 0) ++twoSecondIntervals;
var newDt = new DateTime(twoSecondIntervals * twoSecondsInTicks);
Your problem is in the rounding up to the nearest second line:
// round it UP to the nearest Second
var newDt = originalDt.AddMilliseconds(1000 - originalDt.Millisecond);
you leave intact fractions of milliseconds (since originalDt.Millisecond is a integer value), micro- and nano- seconds; it should be
// round it UP to the nearest Second
var newDt = originalDt.AddTicks( - (originalDt.Ticks % TimeSpan.TicksPerSecond));
when working with ticks, the smallest possible datetime unit, you'll get expected 130790247820000000 without nanoseconds (...8763)

C# - Constantly adding 9 digits

As the title says, I'm developing in C#.
I'd like to generate a number which has more than 9 numbers, so I thought about generating numbers, follow by 9.
For instance, if I want to generate a number which has 10 digits - I'd generate first a number which has 9 numbers, and then a number which has 1 digit.
If I generate the number 20, I'd generate first 2 numbers with 9 digits each, and then a number with 2 digits.
This is what I've tried:
for (int i = 0, j = (int.Parse(inputRandom.Text) % 9 == 0 ? int.Parse(inputRandom.Text) % 9 : int.Parse(inputRandom.Text) % 9 + 1); i < j; i++)
if (i < (int.Parse(inputRandom.Text) % 9 == 0 ? j % 9 : j % 9 + 1) - 1)
numToSend += (BigInteger)r.Next(int.Parse(1 + Zero((int.Parse(inputRandom.Text) % 9 + 8) * (inputRandom.Text.Length - 1))));
else
numToSend += (BigInteger)r.Next(int.Parse(1 + Zero(int.Parse(inputRandom.Text) % 9 * 9)));
The method Zero returns a string with 0, times the number specified in. I.e Zero(1) would return: "0"
I need this method because the method Next(Int32) can return a number, up to what specified between the brackets. I.e Next(10) would return a number between 0 and 9.
My goal
I want to generate a number with a number of digit above 9, and put the number into the string numToSend. I cannot simply do this, because the method Next() can only generate ints.
Even after your edit, I don't find the question very clear. However, I do think I understand that you are essentially trying to generate a random BigInteger value, and that you want the random value to be within a range defined by some specific number of digits.
For example, if you ask for a 10-digit value, some value between 0 and 9999999999 should be returned. If you ask for a 20-digit value, some value between 0 and 99999999999999999999 should be returned.
Based on that understanding, I believe this method should accomplish what you are asking:
BigInteger NextRandom(Random random, int digitCount)
{
BigInteger result = 0;
while (digitCount-- > 0)
{
result = result * 10 + random.Next(10);
}
return result;
}
Note that the number returned could have fewer than digitCount actual digits in it. In fact, there's always a 1-in-10 chance of that happening. And a 1-in-100 chance of the result having fewer than (digitCount - 1) digits, etc.
If you want something different from that, please be more specific about the exact requirements of the output you want (read https://stackoverflow.com/help/how-to-ask for advice on how to present your question in a clear, answerable way).
For example, the above won't generate a random value within some arbitrary range; the (exclusive) max value can only ever be a power of 10. Also, due to the way BigInteger works, if you are looking for a value with a specific number of binary digits, you can do that simply by using the Random.NextBytes() method, passing the resulting array (making modifications as appropriate to ensure e.g. positive values).
See also C# A random BigInt generator for more inspiration (I would've voted as that for a duplicate, but it seems from your description that you might be looking for a slightly different result than what's requested in that question).

How can I randomly generate a date for sometime over the last 30 days?

I have a variable named d that I am currently giving the value:
var a = DateTime.Now;
How can I make it so that the variable gets a different date between now and the last 30 days at random each time the variable is set?
Random r = new Random();
var a = DateTime.Today.AddDays(-1 * r.Next(30));
Note that the Next(30) will never return the value of 30 itself since the Next function parameter is an exclusive value. Your statement "the last 30 days" is a bit ambiguous... If you aren't including "today" in the last 30 days, then you'll just need to add 1 and make it r.Next(31).

Random.Next returns always the same values [duplicate]

This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 9 years ago.
I use the method to generate unique number but I always get the same number -2147483648. Even if I stop the program, recompile it and run again I still see the same number.
public static int GetRandomInt(int length)
{
var min = Math.Pow(10, length - 1);
var max = Math.Pow(10, length) - 1;
var random = new Random();
return random.Next((int)min, (int)max);
}
Try externalizing the random instance:
private readonly Random _random = new Random();
public static int GetRandomInt(int length)
{
var min = Math.Pow(10, length - 1);
var max = Math.Pow(10, length) - 1;
return _random.Next((int)min, (int)max);
}
This is not an issue of not reusing Random instance, the results he gets should be random on multiple starts, not always being -(2^32)
This is the issue with length being too big, and casting powers of length to int. If you break the code into following lines:
var min = Math.Pow(10, length - 1);
var max = Math.Pow(10, length) - 1;
var random = new Random();
var a = (int)min;
var b = (int)max;
return random.Next(a, b);
You'll see that a and b are -2147483648, making that the only possible result of Next(min, max) (the doc specifies if min==max, return min).
The largest length you can safely use with this method is 9. For a length of 10 you'll get System.ArgumentOutOfRangeException, for length > 10 you'll get the -2147483648 result.
You have three problems with your code.
You should externalize your random variable.
You have a problem with truncation error.
The range between min and max is way to large.
The first problem is because you may not have enough time to advance the seed when reinitializing your random variable. The second error comes from truncating your (what would b very large) numbers down to ints. Finally, your biggest problem is your range between your min and your max. Consider finding the range between min and max (as defined in your code) with inputs 1->20:
length max-min
1 8
2 89
3 899
4 8999
5 89999
6 899999
7 8999999
8 89999999
9 899999999
10 8,999,999,999
11 89999999999
12 899999999999
13 8999999999999
14 89999999999999
15 899999999999999
16 9E+15
17 9E+16
18 9E+17
19 9E+18
And keep in mind that the maximum integer is 2,147,483,647, which is passed on any number greater than 9.
You should keep an instance of Random and not new() it up all the time, that should give you better results.
Also check for what length actually is. It may be giving you funny results as to the limits.
I think the problem is the calculation of min and max. They will be greater than Int32.MaxValue pretty fast...
In your class, have one instance of Random, e.g.:
public class MyClass
{
private readonly Random random = new Random();
public static int GetRandomInt(int length)
{
var min = Math.Pow(10, length - 1);
var max = Math.Pow(10, length) - 1;
return random.Next((int)min, (int)max);
}
}
The fact that random always returns the same values only exists for testing purposes.
Random classes usually use a seed to initialize themselves, and will usually return the same sequence provided the seed is the same one :
Always reuse the same Random() instance instead of recreating one over and over again
if you want unpredictable results, use a time-dependent seed rather than an hard-coded one
It's very difficult to code a truly random number generator. Most methods use external entropy generators (such as mouse movement, cpu temperature, or even complex physical mechanisms such as helium balloons colliding one another...).
The Random instance should be created only once and then reused. The reason for this is that the RNG is by default seeded with the current system time. If you rapidly create new Random instances (and pull one value from it) then many of them will be seeded with the same timestamp, because the loop probably executes faster than the system clock advances.
Remember, a RNG initialized by seed A will always return sequence B. So if you create three Random instances all seeded with for example 123, these three instances will always return the same number on the same iteration.

Categories

Resources