Jump to content

Random String Examples?


vailixi

Recommended Posts

So I was thinking about ways to create random directories and file names. I figure first I'll have to create a random string function first. I'm sure there are a ton of ways to do this. So here I'll just post what I came up with. I was messing around with Visual BASIC today. So that's what I coded it in. Any example are great. Don't care what language they are in. I created a string array and just used random numbers to index the array elements, then I concatenated them together to make a single string variable.

I was going to make a random string of a random length by creating a random number and using that to end my loop but VB didn't like the syntax I was using.

Module Module1

    Sub Main()

        Dim StringArray() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
       
        Dim RandomInteger As Integer
       
        Dim String1 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String1 = StringArray(RandomInteger)

        Dim String2 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String2 = StringArray(RandomInteger)

        Dim String3 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String3 = StringArray(RandomInteger)

        Dim String4 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String4 = StringArray(RandomInteger)

        Dim String5 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String5 = StringArray(RandomInteger)

        Dim String6 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String6 = StringArray(RandomInteger)

        Dim String7 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String7 = StringArray(RandomInteger)

        Dim String8 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String8 = StringArray(RandomInteger)

        Dim String9 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String9 = StringArray(RandomInteger)

        Dim String10 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String10 = StringArray(RandomInteger)

        Dim String11 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String11 = StringArray(RandomInteger)

        Dim String12 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String12 = StringArray(RandomInteger)

        Dim String13 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String13 = StringArray(RandomInteger)

        Dim String14 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String14 = StringArray(RandomInteger)

        Dim String15 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String15 = StringArray(RandomInteger)

        Dim String16 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String16 = StringArray(RandomInteger)

        Dim String17 As String
        Randomize()
        RandomInteger = Int(Rnd() * 51)
        String17 = StringArray(RandomInteger)

        Dim Str1 As String = String1 & String2 & String3 & String4 & String5 & String6 & String7 & String8 & String9 & String10 & String11 & String12 & String13 & String14 & String15 & String16 & String17

        Console.WriteLine(Str1)

    End Sub

End Module

So I wrote basically the same function in C++.

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;

string a;
string b;
string c;
string d;
string e;
string f;
string g;
string h;
string i;
string j;
string k;
string l;
string m;
string n;
string o;
string p;
string q;
int RandomNumber1;
int RandomNumber2;
int RandomNumber3;
int RandomNumber4;
int RandomNumber5;
int RandomNumber6;
int RandomNumber7;
int RandomNumber8;
int RandomNumber9;
int RandomNumber10;
int RandomNumber11;
int RandomNumber12;
int RandomNumber13;
int RandomNumber14;
int RandomNumber15;
int RandomNumber16;
int RandomNumber17;


string AlphaRandom[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int main()
{

srand(time(0));

RandomNumber1 = rand() % 51 + 0;
string a = AlphaRandom[RandomNumber1];

RandomNumber2 = rand() % 51 + 0;
string b = AlphaRandom[RandomNumber2];

RandomNumber3= rand() % 51 + 0;
string c = AlphaRandom[RandomNumber3];

RandomNumber4= rand() % 51 + 0;
string d = AlphaRandom[RandomNumber4];

RandomNumber5= rand() % 51 + 0;
string e = AlphaRandom[RandomNumber5];

RandomNumber6= rand() % 51 + 0;
string f = AlphaRandom[RandomNumber6];

RandomNumber7= rand() % 51 + 0;
string g = AlphaRandom[RandomNumber7];

RandomNumber8= rand() % 51 + 0;
string h = AlphaRandom[RandomNumber8];

RandomNumber9= rand() % 51 + 0;
string i = AlphaRandom[RandomNumber9];

RandomNumber10= rand() % 51 + 0;
string j = AlphaRandom[RandomNumber10];

RandomNumber11= rand() % 51 + 0;
string k = AlphaRandom[RandomNumber11];

RandomNumber12= rand() % 51 + 0;
string l = AlphaRandom[RandomNumber12];

RandomNumber13= rand() % 51 + 0;
string m = AlphaRandom[RandomNumber13];

RandomNumber14= rand() % 51 + 0;
string n = AlphaRandom[RandomNumber14];

RandomNumber15= rand() % 51 + 0;
string o = AlphaRandom[RandomNumber15];

RandomNumber16= rand() % 51 + 0;
string p = AlphaRandom[RandomNumber16];

RandomNumber17= rand() % 51 + 0;
string q = AlphaRandom[RandomNumber17];

std::string s = static_cast<std::ostringstream&>(std::ostringstream().seekp(0) << a << b << c << d << e << f << g << h << i << j << k << l << m << n << o << p << q).str();

cout << s;
    return 0;
}

Edited by vailixi
Link to comment
Share on other sites

[*('A'..'Z'), *('a'..'z')].sample(8).join

8chars long random string.

rand(1..9)

This will randomly select a single number.

[*('A'..'Z), *('a'..'z')].sample(rand(3..9)).join

this will generate a sting with a random value and random length(3 through 9)

#ruby

Edited by i8igmac
Link to comment
Share on other sites

That thread about why people hate BASIC? Your code is a MAGNIFICENT demonstration of the issue. It's so awful I want to scratch my eyes out and dissolve them in acid.

Did anybody ever tell you about the wonderful construct called "an array"?

How about "for loop"?

Did you read the documentation on "Randomize()"? (once in the lifetime of the program will suffice)

Link to comment
Share on other sites

Here is a C version.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RANDOM_STRING_LENGTH 17
#define MAXIMUM_ALPHABET_LENGTH 255

int main(int argc, char** argv) {
    char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    char randomString[RANDOM_STRING_LENGTH + 1];
    int i;
    int alphabetLength;

    alphabetLength = strnlen(alphabet, MAXIMUM_ALPHABET_LENGTH);

    srand(time(NULL));

    for (i = 0; i < RANDOM_STRING_LENGTH; i++) {
        randomString[i] = alphabet[rand() % alphabetLength];
    }

    randomString[i] = '\0';

    printf("%s\n", randomString);

    return EXIT_SUCCESS;
}

Link to comment
Share on other sites

As the other Cooper wanted to see more Perl the other day (Ok, he probably didn't want to see more, but as he mentioned it I have to provide some).

#!/usr/bin/perl

use strict;
use warnings;

use constant RANDOM_STRING_LENGTH => 17;

my @alphabet = ('A' .. 'Z', 'a' .. 'z', 0..9);

my $alphabetLength = scalar @alphabet;

my $randomString = "";

for (my $i = 0; $i < RANDOM_STRING_LENGTH; $i++) {
    $randomString .= $alphabet[int(rand() * $alphabetLength)];
}

print "$randomString\n";
Link to comment
Share on other sites

Here is a C version.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RANDOM_STRING_LENGTH 17
#define MAXIMUM_ALPHABET_LENGTH 255

int main(int argc, char** argv) {
    char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    char randomString[RANDOM_STRING_LENGTH + 1];
    int i;
    int alphabetLength;

    alphabetLength = strnlen(alphabet, MAXIMUM_ALPHABET_LENGTH);

    srand(time(NULL));

    for (i = 0; i < RANDOM_STRING_LENGTH; i++) {
        randomString[i] = alphabet[rand() % alphabetLength];
    }

    randomString[i] = '\0';

    printf("%s\n", randomString);

    return EXIT_SUCCESS;
}

This is a great example. Much more concise than what I was doing.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...