CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2004
    Posts
    132

    Same random number every time?

    Hi,

    I have a program for selecting and playing music. One function is a random function to line up 10 random songs from the database. Thing is every time I run the program it generates the same series of random numbers. My code starts by getting the number of songs from the database. So it can generate the random number between 1 and the total number of songs.
    Code:
    Rand = Int((Num_Songs * Rnd) + 1)
    At the moment Num_Songs = 5133 and when I generate the 10 random numbers I get 3622, 2739, 2975, 1487, 1550, 3977, 72, 3905, 4181, 3640 every time. So every time I start the program and press random I get the same 10 songs queue up. What am I doing wrong?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Same random number every time?

    You are missing one small yet major thing.

    Code:
    Randomize
    Rand = Int((Num_Songs * Rnd) + 1)
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Aug 2004
    Posts
    132

    Re: Same random number every time?

    Doh .. thanks

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Same random number every time?

    Quote Originally Posted by Taipan
    Doh .. thanks
    You're welcome. Don't fret it, we all forget little "random" things.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Same random number every time?

    Make sure that you use RANDOMIZE at least once in your program, or you app will pick the same sequence each time you start the app.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Same random number every time?

    Quote Originally Posted by dglienna
    Make sure that you use RANDOMIZE at least once in your program, or you app will pick the same sequence each time you start the app.
    Even better is to use RANDOMIZE only once in your program. For this application is would't really matter, but RANDOMIZE re-seeds the random number generator using a non-random value. So, as counter-intuitive as it seems, the more times you call RANDOMIZE the less randomly distributed the numbers will be.

    BTW,
    Quote Originally Posted by peejavery
    I heard that if you play the Windows CD backwards, you'll hear satanic messages.
    But the most frightening thing is, if you play it forward, it installs Windows!
    LOL
    Last edited by Comintern; May 7th, 2006 at 12:08 PM. Reason: Poor word choice.

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: Same random number every time?

    Quote Originally Posted by Comintern
    LOL
    Yes, I am a Mac user.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured