Thread: Help! Getting identical random numbers?

  1. #1
    Unregistered
    Guest

    Help! Getting identical random numbers?

    In my program I'm getting the same random numbers. I've set the seed with the system time. I can't figure out the problem. Somebody help?

    ------------------------------------------------------------------------------------

    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    #include <iomanip.h>

    void main ()
    {
    int how_many, max, array[40000], count=0, count2, row;

    // Opening Statements

    cout<<"How many numbers? ";
    cin>>how_many;
    while (how_many>40000 || how_many<1)
    {
    cout<<"Please enter a number between 1 and 40000: ";
    cin>>how_many;
    }

    cout<<"How big are the numbers? ";
    cin>>max;

    //Generate random numbers

    for (count=0; count<how_many; count++)
    { srand(time(NULL));
    array[count]=rand() % max + 1;
    }

    row=how_many/10;
    count2=0;

    for (count=0; count<how_many; count++)
    { cout<<array[count]<<setw(7);

    }


    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Well srand should only be called once, so put it before your loop.
    and it should be int main not void main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers
    By mesmer in forum C Programming
    Replies: 4
    Last Post: 10-24-2008, 01:22 PM
  4. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM