How to generate random number [modified]
-
Hello Everybody, I need help for generating Random Numbers...... and need to add current system date with that generated number.... Please help Thanks
modified on Saturday, February 23, 2008 4:45 AM
base64 is an encoding not a number system, what do you need to do exactly? generate a number then encode it ?
Hesham A. Amin My blog
-
base64 is an encoding not a number system, what do you need to do exactly? generate a number then encode it ?
Hesham A. Amin My blog
Hi, its a mistake in writting question. i want to generate random numbers that unique with a combination of current date... means if my random number is 757454584 then its will never generate again so i want some extra digits at the end for unique number that i think the current date 757454584 + 230208 so it will give me the perfect random number... But i am unable to achive this.. can u help me...
-
Hi, its a mistake in writting question. i want to generate random numbers that unique with a combination of current date... means if my random number is 757454584 then its will never generate again so i want some extra digits at the end for unique number that i think the current date 757454584 + 230208 so it will give me the perfect random number... But i am unable to achive this.. can u help me...
-
Hi, its a mistake in writting question. i want to generate random numbers that unique with a combination of current date... means if my random number is 757454584 then its will never generate again so i want some extra digits at the end for unique number that i think the current date 757454584 + 230208 so it will give me the perfect random number... But i am unable to achive this.. can u help me...
Hi, what exactly are you trying to accomplish? Do you need a specific distribution, or a constant length, for example? If you just need some keys that do not collide, i.e. keys which are (almost) unique, you might want to take a look at UUIDs or GUIDs. They are time-based, but in a more subtle manner. Their length is constant. However, they are usually written in hexadecimal strings, e.g.: {2AED1BB2-7314-43e9-9DE7-8AAB3BBC20C1}. Or do you just need something like
Random a = new Random(); string x = String.Format("{0}{1}", a.Next(), DateTime.UtcNow.Ticks);
? yielding, for example, this:"2143552667633393651580403445"
Note that this has a variable length, because
a.Next()
returns some number which can be shorter or longer. Also note that a, if initialized without a seed will use the system time as seed, thus creating completely different numbers on each run (these alone, however, have a rather large chance of colliding). In general, there are no keys which can *never* collide (unless they have infinite legth...), but you can make it extremely unlikely to happen. Hope that helps, Chris
"Obstacles are those frightening things you see when you take your Eyes off your aim" - Henry Ford
Articles Blog -
Hi, its a mistake in writting question. i want to generate random numbers that unique with a combination of current date... means if my random number is 757454584 then its will never generate again so i want some extra digits at the end for unique number that i think the current date 757454584 + 230208 so it will give me the perfect random number... But i am unable to achive this.. can u help me...
Please avoid removing a question that someone already has replied to. It's a bit confusing to read replies to a question that has changed completely. You can strike out text when you edit the question. You can use the Ticks property to get a DateTime value as a number. You should use DateTime.UtcNow rather than DateTime.Now to get the time. The local time is affected by daylight savings time, so it's not unique, i.e. once a year it overlaps for an hour.
string id = myRandomNumber + "_" + DateTime.UtcNow.Ticks.ToString();
But if you want a unique random number, why are you not using a GUID?Despite everything, the person most likely to be fooling you next is yourself.
-
Please avoid removing a question that someone already has replied to. It's a bit confusing to read replies to a question that has changed completely. You can strike out text when you edit the question. You can use the Ticks property to get a DateTime value as a number. You should use DateTime.UtcNow rather than DateTime.Now to get the time. The local time is affected by daylight savings time, so it's not unique, i.e. once a year it overlaps for an hour.
string id = myRandomNumber + "_" + DateTime.UtcNow.Ticks.ToString();
But if you want a unique random number, why are you not using a GUID?Despite everything, the person most likely to be fooling you next is yourself.
-
Hi, its a mistake in writting question. i want to generate random numbers that unique with a combination of current date... means if my random number is 757454584 then its will never generate again so i want some extra digits at the end for unique number that i think the current date 757454584 + 230208 so it will give me the perfect random number... But i am unable to achive this.. can u help me...
You can use GUIDs. or use Cryptography classes (as
RNGCryptoServiceProvider
) to generate the random value. The advantage of usingRNGCryptoServiceProvider
is that you can select the length of the generated value. Here is an example (I choosed the length = 50):using System.Security.Cryptography; . . . byte[] data = new byte[50]; RNGCryptoServiceProvider rand = new RNGCryptoServiceProvider(); for (int i = 0; i < 50; i++) { rand.GetBytes(data); string s = Convert.ToBase64String(data); Console.WriteLine(s); }
Hesham A. Amin My blog