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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Randomized ...

Randomized ...

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 3 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.
  • S Offline
    S Offline
    Sunnygirl
    wrote on last edited by
    #1

    hello @all, i have to write a program which generates randomized letters, e.g. from A to D (variable letters and with a variable length). the string could look like: CString str="AAACCDDDDDBBBBCDABBBDDDD...."; can anybody help me? thank you very much!!! sunny

    D V 2 Replies Last reply
    0
    • S Sunnygirl

      hello @all, i have to write a program which generates randomized letters, e.g. from A to D (variable letters and with a variable length). the string could look like: CString str="AAACCDDDDDBBBBCDABBBDDDD...."; can anybody help me? thank you very much!!! sunny

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      char GetRandomLetter( void )
      {
      char cLetter;

      cLetter = (rand() % 26) + 'A';
      return cLetter;
      

      }

      CString str;
      for (int x = 0; x < some_length; x++)
      {
      char c = GetRandomLetter();
      str += c;
      }

      S 1 Reply Last reply
      0
      • S Sunnygirl

        hello @all, i have to write a program which generates randomized letters, e.g. from A to D (variable letters and with a variable length). the string could look like: CString str="AAACCDDDDDBBBBCDABBBDDDD...."; can anybody help me? thank you very much!!! sunny

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        One solution is to generate a number and then convert it to a char. string text; unsigned int randNum = 0; for (unsigned int i = 0; i < nTextSize; ++i) { randNum = static_cast((((static_cast(rand())) / (static_cast(RAND_MAX + 1))) * 94 + 33)); sName += static_cast(randNum); } Kuphryn

        1 Reply Last reply
        0
        • D David Crow

          char GetRandomLetter( void )
          {
          char cLetter;

          cLetter = (rand() % 26) + 'A';
          return cLetter;
          

          }

          CString str;
          for (int x = 0; x < some_length; x++)
          {
          char c = GetRandomLetter();
          str += c;
          }

          S Offline
          S Offline
          Sunnygirl
          wrote on last edited by
          #4

          thanks for reply. the program works, but i don't know why! DavidCrow wrote: cLetter = (rand() % 26) + 'A'; why does this works? thank you very much! :rose::rose::rose: sunny

          D 1 Reply Last reply
          0
          • S Sunnygirl

            thanks for reply. the program works, but i don't know why! DavidCrow wrote: cLetter = (rand() % 26) + 'A'; why does this works? thank you very much! :rose::rose::rose: sunny

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            rand() gives you a number between 0 and RAND_MAX. Modulo that with 26 gives you a number between 0 and 25. Add 'A' to that gives you one of the 26 letters of the alphabet. Make sense?

            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