Go Back   Gaming Gutter > Non-Gaming > Programming > Source Code


Source Code - Have a source code/project files you want to post? Do so here.

» Site Navigation
» Home
» FAQ
» Log in
User Name:

Password:

Not a member yet?
Register Now!
» Advertisement
» Recent Threads
Go to first new post Why Make us post here?
11-19-2008 03:54 PM
by shep
Last post by Zagoy
Today 11:30 PM
13 Replies, 85 Views
Go to first new post Favorite Junkfood?
10-26-2008 01:36 PM
Last post by Zagoy
Today 11:30 PM
138 Replies, 691 Views
Go to first new post Gaming Gutter secret...
Yesterday 11:32 AM
Last post by ASHTEHCOMMIE
Today 11:26 PM
14 Replies, 105 Views
Go to first new post [S] Legit Neopoints! (:...
Today 06:48 PM
Last post by xzNoobxz
Today 11:21 PM
1 Replies, 13 Views
Go to first new post Whats your favorite song?
08-06-2008 09:53 PM
Last post by maximous
Today 11:17 PM
55 Replies, 383 Views
Reply
 
LinkBack Thread Tools Display Modes

 [C++] Random string - Newbie
Old 08-21-2008, 06:02 AM   #1 (permalink)

Male |G3| is offline

 
|G3|'s Avatar
 
Join Date: Feb 2007
Posts: 3,781
GPoints: 6,415
iTrader: 9 / 100%
|G3| Is a Subordinate to Kyo|G3| Is a Subordinate to Kyo|G3| Is a Subordinate to Kyo|G3| Is a Subordinate to Kyo|G3| Is a Subordinate to Kyo|G3| Is a Subordinate to Kyo|G3| Is a Subordinate to Kyo
Rep Power: 21
[C++] Random string - Newbie

The function "randmG" will generate random letters.
I made this using dev c++, so if you're using Visual then some stuff might be different, I dunno.
I'm still learning C++, so this might not be a good way of generating a random string. So if you know more >>, tell me what I should improve on or w/e.

Code:
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;
string randmG(int lenght);

int main(int argc, char *argv[])
{
    string randm;
    int lent;

    cout << "Please enter how long the random string should be:";
    cin >> lent;
    randm = randmG(lent);
    cout << "Random string generated: " << randm << endl;
    cout << "Press enter to exit.."<< endl;
    cin.get();
    cin.get();
    return 0;
}

string randmG(int lenght)
{
       string G;
       int i = 0;
       int n;
       
       srand(time(NULL));
       for(n=0; n<lenght; n++)
       {
               i = rand() % 26;
               G += (char)(i+65);
       }
       return G;
}
__________________
proxy sites | proxy | web proxy | glype proxy
GAIA FLP GENERATOR


Thanks Hatz ;3

  Reply With Quote

 
Old 08-21-2008, 06:44 AM   #2 (permalink)
Cogito, ergo sum.

Male Check is offline
 
Join Date: Feb 2008
Posts: 1,821
GPoints: 1,005
iTrader: 7 / 100%
Check Is a Lord of AwesomenessCheck Is a Lord of AwesomenessCheck Is a Lord of AwesomenessCheck Is a Lord of Awesomeness
Rep Power: 9
I usually use this.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define STRING_SIZE 32
#define LETTER 26
#define NUMBER 10

static char random_letter(int is_cap)
{
   int letter = (int)(LETTER * (rand() / (RAND_MAX + 1.0)));
   return((char)((is_cap == 1) ? (letter + 65) : (letter + 97)));
}

static char random_number()
{
   int number = (int)(NUMBER * (rand() / (RAND_MAX + 1.0)));
   return((char)(number + 48));
}

static void random_string(int length, char *str)
{
   int i;
   int char_type;
   
   for(i = 0; i < length; i++)
   {
      char_type = (int)(3 * (rand() / (RAND_MAX + 1.0)));
      
      switch(char_type)
      {
      case 0:
         str[i] = random_letter(0);
         break;
      case 1:
         str[i] = random_letter(1);
         break;
      case 2:
         str[i] = random_number();
         break;
      default:
         str[i] = random_number();
         break;
      }
   }  
}

int main(int argc, char *argv[])  
{
   char random_str[STRING_SIZE + 1];

   srand((unsigned)time(NULL));

   random_str[STRING_SIZE] = '\0';

   random_string(STRING_SIZE, random_str);

   printf("Random string %d characters long. [%s]\n", STRING_SIZE, random_str);
   
   return(0);
__________________

Neutral Evil

  Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Powered by vBadvanced CMPS v3.0 RC2

All times are GMT -7. The time now is 11:30 PM.


vBulletin skin developed by: eXtremepixels
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The contents of this webpage are copyright © 2006-2008 GamingGutter.com. All Rights Reserved.

Page generated in 0.09720111 seconds (100.00% PHP - 0% MySQL) with 19 queries