Thread: Query!

  1. #1
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167

    Question Query!

    I recently coded a program called find the hurkle where the program used 2 random numbers to assign the 'hurkle' a place in a 2 dimensional array, and the user had to enter numbers to guess the location of the hurkle. The program would then tell the user to either enter a higher or lower number if the guess was wrong. The only problem with this program was that the rand() function always generated the same 2 numbers when the program was run, ALWAYS!! I do not understand why a random number generator would do this! Can anyone help me with this? .

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    You need to seed the rand() function

    here

  3. #3
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167

    Cheers!

    Cheers mate!

  4. #4
    wondering
    Guest
    I read somewhere that the rand was made that way so that if somebody wants to test your program, they'll get the same result as you did. I hope somebody can explain this better.

  5. #5
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    I read somewhere that the rand was made that way so that if somebody wants to test your program, they'll get the same result as you did. I hope somebody can explain this better.
    So do I!!

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    46
    You probably already know this but I'll pretend I'm saying something interesting anyway (-;

    Random number generators aren't. What they do is generate a seemingly random sequence of numbers based upon a deterministic but difficult to predict algorithm. It's NOT like shuffling a deck of cards, it's more like having an extremely huge deck of cards (millions or billions of cards) preshuffled for you. Well, even if the deck is preshuffled you will be able to predict, at least the beginning of, the sequence eventually. So what we do is jump into this preshuffled deck at seemingly random places, i.e. difficult to predict. This furthers the illusion of randomness without doing any real shuffling, we call this jumping in point the seed.

    People generally use the system timer because it's available & difficult to predict but you could use anything else availble to the computer that seems to jump unpredicatably like counting page swaps, or the rainfall in Idaho. Strictly speaking, time only increases in the computer but it will seem random enuf because: 1) How much the timer increases will be hard to predict. AND more imprtantly 2) The preshuffling at a higher points in the sequence should (if it's a good generator) bring you back to lower numbers (loop back to) relatively quickly. Therefore, in a good enuf sort of way, it will look like you're jumping into the sequence anywhere high or low.

    Now the punchline... when debugging it's vitally imporant to be able to replicate errors. So with random number generators we either turn them off & generate a know sequence or jump into the preshuffled sequence at known points. And in the later case because the sequence is fully deterministic (known in advance from initial inputs) we can be sure that we hit the same problem numbers at the same place in the program. Thus allowing us to get to the problems in the program again & again. Also, if the same random# generator is used on different computers you can use the same seed & get to the same point in the preshuffle & see if or how the proggie breaks on different computers.

    zzzz.....
    Last edited by rafe; 10-11-2002 at 10:05 AM.

  7. #7

  8. #8
    wondering
    Guest
    I think they write the program without a seed, so that it is very repeatable and can be tested bit by bit, and then after everything seems to be working, they add in the seed, and hopefully it will still work, now in a truly random way.

  9. #9
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    Oh...I get it now! I did not get this however:
    You probably already know this but I'll pretend I'm saying something interesting anyway (-;

    Random number generators aren't. What they do is generate a seemingly random sequence of numbers based upon a deterministic but difficult to predict algorithm. It's NOT like shuffling a deck of cards, it's more like having an extremely huge deck of cards (millions or billions of cards) preshuffled for you. Well, even if the deck is preshuffled you will be able to predict, at least the beginning of, the sequence eventually. So what we do is jump into this preshuffled deck at seemingly random places, i.e. difficult to predict. This furthers the illusion of randomness without doing any real shuffling, we call this jumping in point the seed.

    People generally use the system timer because it's available & difficult to predict but you could use anything else availble to the computer that seems to jump unpredicatably like counting page swaps, or the rainfall in Idaho. Strictly speaking, time only increases in the computer but it will seem random enuf because: 1) How much the timer increases will be hard to predict. AND more imprtantly 2) The preshuffling at a higher points in the sequence should (if it's a good generator) bring you back to lower numbers (loop back to) relatively quickly. Therefore, in a good enuf sort of way, it will look like you're jumping into the sequence anywhere high or low.

    Now the punchline... when debugging it's vitally imporant to be able to replicate errors. So with random number generators we either turn them off & generate a know sequence or jump into the preshuffled sequence at known points. And in the later case because the sequence is fully deterministic (known in advance from initial inputs) we can be sure that we hit the same problem numbers at the same place in the program. Thus allowing us to get to the problems in the program again & again. Also, if the same random# generator is used on different computers you can use the same seed & get to the same point in the preshuffle & see if or how the proggie breaks on different computers.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What they mean to say is that nothing is truely random. It's based on some nifty mathmatical formulae to simulate random number ranges. However, it isn't purely random.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    That was a lot easier to digest than the story!!

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    46
    OK OK, lesson learned. I won't make any more of these posts... sorry.

  13. #13

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple query language
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 05:29 PM
  2. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  3. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  4. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 10:44 AM
  5. WBEM Query too slow
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 05:28 PM