Thread: Program with rand() func produces same results every run

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    Program with rand() func produces same results every run

    Hi,

    Basically I am working on a program that uses the rand() function to obviously produce random numbers. These numbers are assigned to various integer variables throughout the program.

    The thing is, every time I run the program I get the same random numbers for each variable that calls the function - they never change. It's almost as if they are being stored somewhere in memory. Changing the name of the variable or putting it in a different place in the code will result in a different number generated, but that is hardly practical.

    I am just wondering if there is something I am missing with its usage?

    Any help is appreciated.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    in the beginning of main, seed the random number generator:
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    
    int main()
    {
            srand(time(0)); //seed the random number generator
                            //based off the system clock
            for(int i=0;i<10;i++)
            {
                    std::cout<<rand()<<'\n';
            }
            return 0;
    }
    Last edited by major_small; 07-11-2005 at 03:08 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    Thank you very much, it works fine now.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    no problem
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 02:01 AM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM
  5. See Program Results
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-07-2002, 12:18 AM