DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Location
    Singapore
    Posts
    10

    Angry random character

    hi all,

    im a newbie of c++ & struggling with a socket programming assignment. my assignment requie me to generate 4 random char with "\" after it
    eg: char msgsent[1000]={"asdk\","ngwq\","utre\"............}

    i need to generate 1000 set of it. is there a smarter way to generate it using C++ instead of manually set??

    all these set of "data" need to send across to a server thru SEND().

    Please help here..... thanks alot.

  2. #2
    Join Date
    Dec 2003
    Location
    Okla, US
    Posts
    126
    First I would make an array populated with letters.

    Then set a random seed using srand(time())

    Then in a loop, get a random number( using rand() ). Mod that number by the length of your array. That result will be the index to get a character.

    Did that make any sense?
    gorshing
    newb

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    if you're not wary of using STL (your shouldn't be!), you can use the random_shuffle() algorithm:
    http://gethelp.devx.com/techtips/cpp.../10min1299.asp
    Notice the "Using random_shuffle() with Built-in Arrays" section.
    Danny Kalev

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links