Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

srand((unsigned)time(NULL)) is too slow!

RehevkorRehevkor Posts: 41Member
I have an MFC app where I want random numbers to be generated as fast as the user can click the button for it. Unfortunately, the numbers only change in 1 second intervals, so they won't change on every click. How can I speed up the seeding?

Comments

  • Eric TetzEric Tetz Posts: 2,141Member
    : I have an MFC app where I want random numbers to be generated as fast as the user can click the button for it. Unfortunately, the numbers only change in 1 second intervals, so they won't change on every click. How can I speed up the seeding?

    You only seed the random generator once, before your first use of 'rand()'.

    rand() always produces the same sequence of numbers given the same seed value. If you reseed before every call to 'rand', you will always get the same number. In your case, that number will change once a second, because that's how often your seed value changes.

    Cheers,
    Eric
  • RehevkorRehevkor Posts: 41Member
    I have an MFC app where I want random numbers to be generated as fast as the user can click the button for it. Unfortunately, the numbers only change in 1 second intervals, so they won't change on every click. How can I speed up the seeding?
    :
    : You only seed the random generator once, before your first use of 'rand()'.
    :
    : rand() always produces the same sequence of numbers given the same seed value. If you reseed before every call to 'rand', you will always get the same number. In your case, that number will change once a second, because that's how often your seed value changes.
    :
    : Cheers,
    : Eric
    :

    Ok, is there any way I can speed up the rate at which the seed value changes?
  • WickedMageWickedMage Posts: 21Member
    I had the same problem in an MFC program of mine. I assume that you are creating a dialog based program. You can place srand(..) in the creation of your dialog. On the function void CGameDlg::OnButton()you 'll have to put only rand(). This will genarate you a unique number, no matter how frequent the user hits the button.
    Let 's say that your dialog is called GameDlg. In GameDlg.cpp file you will find in the beginning something like this:

    CGameDlg::CGameDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CGameDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CGameDlg)
    //}}AFX_DATA_INIT

    /*YOU CAN PLACE srand(..) HERE, AS WELL AS ANY VARIABLE INITIALIZATION YOU WANT*/
    }

    I hope this will help you.

    WickedMage
  • RehevkorRehevkor Posts: 41Member
    I don't understand... I have to use rand() more than once, because I have it inside a for loop. I need it to generate 6 random numbers each time the button is clicked, which it does fine, but I need it to do this EVERY time the button is clicked. As it is, I can click the butron rapidly (several times per second) yet it will only generate new random numbers in one second intervals. Can I somehow make that interval smaller, or force a new random seed when the button is clicked, regardless of the timeframe?
  • RehevkorRehevkor Posts: 41Member
    That did it! Simple as moving a single line of code, should have known :) Thanks a bunch Wiz, and the rest of you too.

    For the record, I'm working on a 3E dungeons and dragons character creator. This is for the dice simulator that will be in it.
  • DariusDarius Posts: 1,666Member
    : I don't understand... I have to use rand() more than once, because I have it inside a for loop. I need it to generate 6 random numbers each time the button is clicked, which it does fine, but I need it to do this EVERY time the button is clicked. As it is, I can click the butron rapidly (several times per second) yet it will only generate new random numbers in one second intervals. Can I somehow make that interval smaller, or force a new random seed when the button is clicked, regardless of the timeframe?
    :

    I don't know what you are thinking (though I think I thought something similar back when I programmed in BASIC) but as far as I know for it's typical application you should only call srand() ONCE PER PROGRAM EXECUTION. Calling srand() multiple times won't make it more random or won't reinitialize the randomness, in fact it probably destroys the randomness.

    The only time I've called srand() multiple times in a program execution was when I wanted to REPEAT a set of random numbers.

    //I'm just making these numbers up, it's more random than rand()
    srand(5)
    rand()=361
    rand()=8763
    rand()=9
    srand(5)
    rand()=361
    rand()=8763
    srand(5)
    rand()=361
    rand()=8763
    rand()=9
    rand()=126



    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • WickedMageWickedMage Posts: 21Member
    : That did it! Simple as moving a single line of code, should have known :) Thanks a bunch Wiz, and the rest of you too.
    :
    : For the record, I'm working on a 3E dungeons and dragons character creator. This is for the dice simulator that will be in it.
    :

    Funny this was the first project of mine that I used srand() and rand(). The other was something like MTG.

    WickedMage
Sign In or Register to comment.