Hi,
I need a script that will generate a set of random strings in sequence, with the ability to predetermine the length, quantity, and alphabet of individual string, and to use the outputs of earlier strings in the sequence to define the parameters of later strings. For examples, I might want to generate a one-character string with an alphabet of {1,2,3,4}, followed by X one-character strings with the alphabet {0,1}, where X equals the output of the first string. Unfortunately I don't know too much about code -- I'm not a programmer, this is for a sociolinguistics project -- but if anyone has any relatively easy ideas as to how to make this happen I'd much appreciate it.

Thanks
V

Recommended Answers

All 7 Replies

Hi,
I need a script that will generate a set of random strings in sequence, with the ability to predetermine the length, quantity, and alphabet of individual string, and to use the outputs of earlier strings in the sequence to define the parameters of later strings. For examples, I might want to generate a one-character string with an alphabet of {1,2,3,4}, followed by X one-character strings with the alphabet {0,1}, where X equals the output of the first string. Unfortunately I don't know too much about code -- I'm not a programmer, this is for a sociolinguistics project -- but if anyone has any relatively easy ideas as to how to make this happen I'd much appreciate it.

Thanks
V

I'd love to help - really I would. But - I don't really understand what you're trying to accomplish.

Why don't you give some example of the EXPECTED output of this routine?

Sure thing, opa6x57, thanks for the help.

Running the script would yield a series of strings. The parameters of the first might be something like:

Parameter1: Length=1, Count=1, alphabet={1,2,3,4,5}

Say this yielded the string /4/

The parameters of the next string (or sub-series of strings) might look like:

Parameter2: Length=[output of Parameter1] Count=[output of Parameter1], alphabet={a,b,c,d}

So based on the output for the first string [Parameter1] above (/4/), output here might look like:


aabd
cbad
cbbd
abac

Therefore, the output of running the script as a whole once would be:

4
aabd
cbad
cbbd
abac


So the idea is to make a script that creates a series of customized strings that uses outputs of earlier stings in the parameters of later strings.

Does that help to clarify?
Thanks,

V

Sure thing, opa6x57, thanks for the help.

Running the script would yield a series of strings. The parameters of the first might be something like:

Parameter1: Length=1, Count=1, alphabet={1,2,3,4,5}

Say this yielded the string /4/

The parameters of the next string (or sub-series of strings) might look like:

Parameter2: Length=[output of Parameter1] Count=[output of Parameter1], alphabet={a,b,c,d}

So based on the output for the first string [Parameter1] above (/4/), output here might look like:


aabd
cbad
cbbd
abac

Therefore, the output of running the script as a whole once would be:

4
aabd
cbad
cbbd
abac


So the idea is to make a script that creates a series of customized strings that uses outputs of earlier stings in the parameters of later strings.

Does that help to clarify?
Thanks,

V

Okay - now I have an understanding ... what have you got, so far?
Unix? Linux? Which shell?

UNIX - wish I knew enough about code to say more but I'm basically starting from scratch. Thanks.

UNIX - wish I knew enough about code to say more but I'm basically starting from scratch. Thanks.

I'm gonna have to bow out ... without knowing for certain which shell you're using. Which flavor of UNIX this is for - there are too many variables.

Sorry.

So, UNIX on a mac in C++ in Xcode -- does this help?

#!/bin/bash
## Name: randstring
## Synopsis: randstring [ -n NUM ] [ -l LENGTH ] [ -c CHARSET ]
## Author: Chris F.A. Johnson

## Defaults:
length=4
num=4
charset=abcdefghijklmnopqrstuvwxyz

rand_char()
{
  RAND_CHAR=${charset:$RANDOM % ${#charset}:1}
}

rand_word()
{
  local string=
  while [ ${#string} -lt $length ]
  do
    rand_char
    string=$string$RAND_CHAR
  done
  printf "%s\n" "$string"
}

while getopts l:n:c: opt
do
  case $opt in
    l) length=$OPTARG ;;
    n) num=$OPTARG ;;
    c) charset=$OPTARG ;;
  esac
done
shift "$(( $OPTIND - 1 ))"

n=0
while [ $(( n += 1 )) -le $num ]
do
  rand_word
done
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.