Codeforces и Polygon могут быть недоступны в период с 23 мая, 7:00 (МСК) по 23 мая, 11:00 (МСК) в связи с проведением технических работ. ×

Блог пользователя Sakalya

Автор Sakalya, 12 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
12 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      12 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

      • »
        »
        »
        »
        12 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
12 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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