Trying to make rand different per program run

This is a discussion on Trying to make rand different per program run within the C++ Programming forums, part of the General Programming Boards category; Hello everybody, first off I'd like to say thank you to everyone who helped me with my while loop dilema ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Post Trying to make rand different per program run

    Hello everybody, first off I'd like to say thank you to everyone who helped me with my while loop dilema two weeks or so ago.

    I am now in the process of expanding the functionality of my guessing program so that it will generate random numbers which are different each time the program is run, I would ideally like to be able to set a range and then compile, as it is now my numbers are constrained only by the limits of the unsigned short

    I would like to be able to state an exact range of numbers for example "X-XXXXX" or "X-XX" or "X-XXX" and so on.

    I am using bloodshed dev c++ 4 and have used the rand() function successfully to generate a random number however I have not been able to control the range of the number. Anther big problem is that the number doesn't change everytime the program is run, I would like the a unique random number to be generated everytime the program is run.


    I have remarked by attempts in this venture thus far in the source code below.

    Here is my source code:

    #include <iostream>
    #include <string>
    #include <stdlib.h>


    using namespace std;

    int main()
    {


    unsigned short correctguess;
    unsigned short attempt;

    //This is what i'v tried so far in order to get a DIFFERENT random number
    //everytime the program runs...

    //srand (correctguess);
    //int rand(srand n+2);

    //And the above has not worked.


    rand (correctguess);
    //rand is 18467 everytime the program runs.
    correctguess = rand();




    cout << "Pick a number between 0 and 65535:" << endl;

    cin >> attempt;

    if (attempt == correctguess) //First try win contingency
    cout << "Thats right on the money!" << endl;


    while (attempt != correctguess) //Begining of guessloop
    {

    while (attempt > correctguess) //Begining of high rejection
    {
    cout << "Try a little lower." << endl;
    cin >> attempt;
    } //End of high rejection

    while (attempt < correctguess) //Begining of low rejection
    {
    cout << "Thats too low." << endl;
    cin >> attempt;
    } //End of low rejection

    } //End of guessloop

    cout << "Thats right on the money!" << endl; //Normal win message
    return 0;
    }

    I would really appreciate any help I recieve and thank you very much!

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    To get a different random # everytime, use this code for random numbers

    Code:
    #include <time>
    .....
    srand(time(NULL));
    number=rand();

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    It worked

    Thank you!


    Now please tell me where I went wrong?

    Pasting code, having it work and then leaving it alone just doesn't sit right with me unless I know what it is doing...

    Any reference you can point me to?

    Or just a brief decription of what time does?

    Incidentally, #inlcude <time> didn't work in dev c++ 4 however #include <time.h> did work.

    Do I need to use the time header when I want a number to be shuffled differently each time?
    And how do I control the range?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    search for random numbers on these boards and check a few threads. eventually you will come across a nice formula that will take care of all your random number questions.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    rand() is a pseudo-random number generator. It will create the same sequence of random numbers with a given seed. The one used in srand(). So you typically use time() as the seed to srand() so the sequence will be different each time the program runs.
    There are advantages to having the same sequence though in programming. You can create large worlds without the large space required to hold all of the data.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    I see, so time() tells rand to use a different seed per program run or per each time the program runs...

    Very good, I will search the board(s) right now and see what I can find...

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Look here in the FAQ for range issues...

    http://www.cprogramming.com/boardfaq.html#random

    time() returns the time. What you need with a pseudo random number sequence generator is a "seed". If you give the generator the same seed each time, it will generate the same sequence. If you call time() at exactly the same time on two different machines, (like EXACTLY the same time), they will still produce the same random number sequence.

    Look up time() in your compilers help.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

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, 02:42 AM
  2. Help on making program run more efficiently
    By peeweearies in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 03:01 AM
  3. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 06:32 PM
  4. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  5. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 01:00 PM

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