Thread: producing random numbers???

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    16

    Question producing random numbers???

    I have to produce a random number and can't figure it out. It gives me the same number each time. This isn't the homework. She told us how to produce the random number and i can't figure it out. The program I am writing for class is to learn how to use if statements.

    I'm supposed to call srand once with a large prime number: srand(2707). Then it goes on to say that when you call srand with time as seed: srand(time(NULL)) the seed and the number will be different each time you run the program.

    Here is the code that I have:

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

    // I was told to include these three, should MAX_NUMBER be float?
    void srand( unsigned int seed );
    int rand ( void );
    const float MAX_NUMBER = 20.0;
    //

    int main()
    {
    int number;

    srand ( time( NULL ));

    //I was also told to include this one.
    number = int((float(rand() ) / float( RAND_MAX )) * MAX_NUMBER );
    //
    cout << number << endl;


    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    16
    got it to work!!

  3. #3
    Morpheus
    Guest

    STDLIB.h

    Use this code it will work i ganantee it

    #include <stdlib.h>

    int main()
    {
    randomize();
    int RandomNumber = random(10); // Will display random number from 0-9

    YOUR CODE

    return(0);
    }

    If this doenst word your stdlib.h is corrupted

    http://home.dencity.com/neoprogramming/index.html

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    My stdlib.h has no function named random(); or randomize();

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