Difference in Unseeded rand() and seeded rand()

This is a discussion on Difference in Unseeded rand() and seeded rand() within the C++ Programming forums, part of the General Programming Boards category; Hello! Hey, im a beginning c++ student, and had a very complex, probably simple to you guys tho. I was ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Difference in Unseeded rand() and seeded rand()

    Hello!

    Hey, im a beginning c++ student, and had a very complex, probably simple to you guys tho. I was just wondering what the difference between a seeded random number generator and a unseeded random number generator in C++.

    I ask this becuase i was assigned a program where an array stores 1000 numbers from each generator, then i have to pass the information to main to show and output.

    Please, i really would like to know the answer so i can start coding!

    Thanks!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    You can set the seed for the rand() funtion by calling srand() once before any calls to rand(). I guess omitting the call to srand() means it's unseeded.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    rand() produces a series of numbers based off how its seeded. By using different seed values you get a different series of numbers. Saying it is unseeded is really a misnomer as it does have some default seed value. So by never calling srand() you will get the same series of numbers on every run of your program.

  4. #4
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Imagine srand(1); happening behind your back...

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    It'd be like White Chapel all over again.
    Sent from my iPadŽ

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was just wondering what the difference between a seeded
    >random number generator and a unseeded random number generator in C++.
    The random number generator should still work, if that's what you're asking. If it's a very sophisticated RNG then it likely uses an array for seeding, in which case not seeding the generator could cause it to fail. In the case of less sophisticated generators, or generators with guaranteed semantics like std::rand, there's always a default seed and the generator will always work, but the default never changes so you'll get a predictable sequence (useful for debugging!). In the case of std::rand, the default seed is the same as if you called srand ( 1 ).
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21