Thread: srand() no working as expected

  1. #1
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179

    srand() no working as expected

    Hey, thanks everyone for being here. I know I don't stop in as often as I should and I hope you'll forgive me for only dropping in when I have a problem. But with questions like this maybe it's obvious why I don't try to solve other folks problems.

    I create a program that generates random puzzles. It does so with a lot of calls to rand(), no funny business. After I finished it I thought "wouldn't it be great if you could re-generate any puzzle you liked simply by inputting the seed that was used before the first call to rand(). So I fiddle things around, store the seed that's used in a new variable, and call srand just before any rand() calls to generate the puzzle. The problem is it's not generating the same puzzle every time!

    I'm having it output the code that includes all necessary dimension variables as well as the seed after generation, so I know the seed is the same. Two different puzzles, same code. Sometimes it works. Sometimes it doesn't.

    Is there something I don't know about that could effect the output of rand()?

    I'm using Code::Blocks with MinGW.

    The code is attached below. It's the numbrixgen.h and numbrixgen.c that has the srand and rand calls in question, and the main.c where it's called. The utils is not a part of this discussion but included so you could compile it if you were so inclined.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't call srand() before every call. Call it once at the beginning of the program. Call it with some generated value (typically the current time) to create a new puzzle, or with the value from a command line argument to recreate an old puzzle.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    No, not before every call. Good grief. Just before I generate a puzzle.

    And for some reason this morning it's working I can reliably generate puzzles from a code. Figures.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Porting a shared library from Linux to Windows
    By DARKGuy in forum C++ Programming
    Replies: 1
    Last Post: 06-23-2007, 01:45 PM
  2. Simple vector class problem, please help
    By fidodidohk in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2007, 09:13 AM
  3. Vector not working!!
    By aaroroge in forum C++ Programming
    Replies: 4
    Last Post: 07-16-2005, 05:17 PM
  4. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM