Sakalya's blog

By Sakalya, 12 years ago, In English

All of us know very well,what a random number is??Most of you know how to generate random numbers using C/C++,And you may also have observed that the method you used to generate a random number(i.e.Use of rand() function) generates the same answer every time you run the program. The reason behind generation of the same random number is the seed value which is being used by your program doesn't change.A seed value is a special number which determines where the random number generator should start generating values.If you don’t seed the random number generator, it will generate the same exact sequence of numbers every time you run it. Hence instead of using normal rand() function in C/C++,you should use srand() function to generate a random number.But this srand() function must be provided with a non constant seed value,so that it will become a pure random geneartor function,and the best non constant seed value will be system time,hence we will use time(0) function which returns current system time which will be always non-constant. Hence,to generate a a pure random number in C/C++,we should use srand(time(0)) function,and we all know to generate a random number in the given range we will use modulo.

  • Vote: I like it
  • +2
  • Vote: I do not like it

| Write comment?
»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

you should be very careful with srand(time(0)), because the smallest unit is a second, but one second is a big time for CPU... When i needed to get a lot of "random" numbers, i wrote a function, which return random number, but it was not working. Every time, when i used this function in my programm, it return same result. I had the same "random" numbers during a second. After this i spent much time for create another function.My new function worked with STL(random_shuffle()). It always return correcr result, but it was very very slow. Recently i found my bug. In my function, which generates numbers, was "srand(time(0))", that's why, my first function gave same "random" numbers. // Sorry for my English, thats my first comment on foreign language.

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You should run srand(time(0)) just once in your main function and not in your random generator function.

    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Then for generating next random number,which non-constant seed can I use??????

      • »
        »
        »
        »
        12 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Use seed once ( at the beginning of your program), after this use rand() function or other proper in your language. However, due to D. Knuth ( the Art of Programming), native random generators are too bad. He proves it and offers some other implementations.

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

By the way, on some online judges time(0) can be prohibited as system call. Be carefull

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    So can you suggest any better non-constant seed value??????

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You may use any nonstandart random seed as seed generated by time()