Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help!

Help!

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialc++loungelearning
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaveE9th
    wrote on last edited by
    #1

    I'm trying to learn how to generate a random number (for a jukebox to play MIDI songs). I bought the C++ standard library reference book and it explains what rand( ) does, but I've not been able to get it to work yet. I don't see any complete examples. I have several books, but they don't talk about rand( ). I also have discovered the random_shuffle function as well, but don't know how to make it work. The biggest problem I've had so far with programming is to implement the code for the various functions. Anyway, I was hoping someone(s) would give me a simple working example of both 1. rand( ) 2. random_shuffle( ) I would appreciate it much, Thanks...Dave :-D

    "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

    D 1 Reply Last reply
    0
    • D DaveE9th

      I'm trying to learn how to generate a random number (for a jukebox to play MIDI songs). I bought the C++ standard library reference book and it explains what rand( ) does, but I've not been able to get it to work yet. I don't see any complete examples. I have several books, but they don't talk about rand( ). I also have discovered the random_shuffle function as well, but don't know how to make it work. The biggest problem I've had so far with programming is to implement the code for the various functions. Anyway, I was hoping someone(s) would give me a simple working example of both 1. rand( ) 2. random_shuffle( ) I would appreciate it much, Thanks...Dave :-D

      "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

      D Offline
      D Offline
      Dave Bryant
      wrote on last edited by
      #2

      // Seed the random number generator srand( time(0) ); // This is the highest number we want const int nMaxNum = 1234; // Generate it int nNum = rand() % nMaxNum + 1; rand() gives us a number between 0 and RAND_MAX (which if i recall correctly is ~32768 on VC6), so to convert it to the range we want, we divide it by the max number and take the remainder. random_shuffle() randomises the order of components in a random access container such as a vector - you should not be using this function to generate a random number, but instead you could insert the records for all the songs into a vector, and then call random_shuffle on the vector to randomise it. e.g. struct Record {   string sName;   // Data for the song itself }; vector vSongs; // Add some songs vSongs.push_back( record1 ); vSongs.push_back( record2 ); // etc... // Randomise them random_shuffle( vSongs.begin(), vSongs.end() ); Dave http://www.cloudsofheaven.org

      D 1 Reply Last reply
      0
      • D Dave Bryant

        // Seed the random number generator srand( time(0) ); // This is the highest number we want const int nMaxNum = 1234; // Generate it int nNum = rand() % nMaxNum + 1; rand() gives us a number between 0 and RAND_MAX (which if i recall correctly is ~32768 on VC6), so to convert it to the range we want, we divide it by the max number and take the remainder. random_shuffle() randomises the order of components in a random access container such as a vector - you should not be using this function to generate a random number, but instead you could insert the records for all the songs into a vector, and then call random_shuffle on the vector to randomise it. e.g. struct Record {   string sName;   // Data for the song itself }; vector vSongs; // Add some songs vSongs.push_back( record1 ); vSongs.push_back( record2 ); // etc... // Randomise them random_shuffle( vSongs.begin(), vSongs.end() ); Dave http://www.cloudsofheaven.org

        D Offline
        D Offline
        DaveE9th
        wrote on last edited by
        #3

        Thanks Dave, I'll give it a try. I was trying to use the get_time( ) function, but didn't think of doing it the way you did. Is get_time something entirely different? Thanks much, Dave :-D

        "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

        D 1 Reply Last reply
        0
        • D DaveE9th

          Thanks Dave, I'll give it a try. I was trying to use the get_time( ) function, but didn't think of doing it the way you did. Is get_time something entirely different? Thanks much, Dave :-D

          "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

          D Offline
          D Offline
          Dave Bryant
          wrote on last edited by
          #4

          By the look of things, get_time() retrieves a time from an input stream - but i have never used it so i'm not sure. time() retrieves the time from the system clock instead. Dave http://www.cloudsofheaven.org

          D 1 Reply Last reply
          0
          • D Dave Bryant

            By the look of things, get_time() retrieves a time from an input stream - but i have never used it so i'm not sure. time() retrieves the time from the system clock instead. Dave http://www.cloudsofheaven.org

            D Offline
            D Offline
            DaveE9th
            wrote on last edited by
            #5

            Thanks for the great example Dave, I'll spend some time with it. I did get the rand() working. Thanks, Dave :-D

            "The man who reads nothing is better educated than the man who reads nothing but newspapers."- Thomas Jefferson

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups