Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to generate random number [modified]

How to generate random number [modified]

Scheduled Pinned Locked Moved C#
helptutoriallounge
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    D i x y
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • D D i x y

      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

      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • H Hesham Amin

        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

        D Offline
        D Offline
        D i x y
        wrote on last edited by
        #3

        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...

        G C G H 4 Replies Last reply
        0
        • D D i x y

          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...

          G Offline
          G Offline
          Gareth H
          wrote on last edited by
          #4

          Dikshant,

          Random random = new Random();
          string randomString = (DateTime.Now.ToString() + "_" + random.Next().ToString());
          random.Dispose();

          Above gives you "23/02/2008 11:08:23_3243243242"

          1 Reply Last reply
          0
          • D D i x y

            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...

            C Offline
            C Offline
            Christoph Menge
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • D D i x y

              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...

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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.

              D 1 Reply Last reply
              0
              • G Guffa

                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.

                D Offline
                D Offline
                D i x y
                wrote on last edited by
                #7

                Thank you all for solution.... And Sorry that i have to edit it..... Again thanks Dikshant

                1 Reply Last reply
                0
                • D D i x y

                  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...

                  H Offline
                  H Offline
                  Hesham Amin
                  wrote on last edited by
                  #8

                  You can use GUIDs. or use Cryptography classes (as RNGCryptoServiceProvider) to generate the random value. The advantage of using RNGCryptoServiceProvider 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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups