Friday 1 November 2013

Generate True and Unique Random Number in a C Program


Hello,
           
Have you ever tried to generate a random number in C Program?
           
And face the problem that it gives same iterative numbers every time you run the program?

Now, let's say you need to generate a password which is different every time. How would you do it?

For example, a program like this:
would generate output like:


But there is a problem here (or the solution), to generate a "truly" random number, we need to provide unique seed to that function.

Here the question is: "What can be so unique in our world, that it will never be repeated?" And the answer is "TIME". So, current system time is provided as seed to generate truly random numbers. Code snippet:


After adding this, here is the snapshot of this program:

So, here you generated a unique random number.

Thanks for reading. Leave Comments.

EDIT : Here is a full term project based on this. :)

Hardik Madhu

1 comment:

  1. youtube5k youtube5k youtube
    youtube5k youtube5k youtube5k youtube is a website that you can download, play, play movies, photos, music and more. This website provides free video and best youtube to mp3 converter other

    ReplyDelete