Generating random characters from strings

In summary, the conversation discusses how to write a program that takes a positive integer and a string as input and generates a new string with a length equal to the integer, composed of random characters from the original string. Various methods are suggested, including using for loops, random number generators, and arrays. The final solution involves using a random number generator to select characters from the original string and print them out to create a new string. The potential issue of selecting the same character position multiple times is raised.
  • #1
Hiche
84
0

Homework Statement



Write a program that takes a positive integer N and a string as command line arguments (N is assumed to be smaller than the length of the string). The program should pick N random characters from the string and construct and print a new string composed of these random characters.

Homework Equations



for loops. random(?)

The Attempt at a Solution



We so far learned how to generate random double variables using Math.random(). Is there a way (method) to generate random characters in strings?

Code:
<method signature><method name><parameters> 

for i = 0; i < some integer; i++
       string.random.charAt(i); // not really a command but something as this exists?
 
Physics news on Phys.org
  • #2
It depends on the language, but you have to use random number as an index to the string. Something like - for example - string[rand()] - it won't work, but should push you in the right direction.
 
  • #3
A more descriptive title would be, "Choosing N characters at random from a string of k characters". I imagine that you are not allowed to choose the ith character more than once, for any i?

Random numbers are fascinating. Choosing things at random is fun to program.

A foolproof method is to set up an array A[1]...A[k] storing the numerals 1..k in that order. Then shuffle (i.e., randomly mix up or swap around) the elements of array A. (I'll leave you to think about how to shuffle.)

Then simply print out the characters in your string that correspond in position to the numbers found in the first N elements of array A (because they have now been well shuffled).

This way, you aren't making any great demands on your random number generator, and you can monitor everything along the way to make sure it is working out exactly as you planned. Yes, you can plan random events! :smile:
 
  • #4
Unfortunately, we haven't covered arrays in the course so using them will result in a failing grade at the moment. But thank you for the answer; this will sure come in handy when we take arrays. I actually thought of it and I think it worked:

Code:
public class RandomString
{
	public static void main(String[] args)
	{
		int n = Integer.parseInt(args[0]); // n should be less than the length of the string
		String str = args[1];
		
		for (int i = 1; i <= n; i++)
		{
			int position = randomGenerator(str.length());
			char c = str.charAt(position);
			System.out.print(c);
		}
	}
	
	public static int randomGenerator(int n)
	{
		double rand = Math.random();
		double num = 1 + rand * (n - 1); // the string's length starts at 1 hence we add 1
		int position = (int) num; 
		return position;
	}
}

The output blabbered a string of random characters, so I'm guessing it worked.
 
  • #5
Two comments. Your code doesn't actually "construct ... a new string". Also, there is nothing to prevent your program selecting the same character position multiple times; is this likely to be a problem?
 

1. How can I generate random characters from a string in Python?

In Python, you can use the random.choice() function to generate a random character from a given string. You can also use a for loop to iterate through the string and use the random.choice() function to generate multiple random characters.

2. Is there a way to generate only unique random characters from a string?

Yes, you can use the random.sample() function in Python to generate a list of unique random characters from a given string. This function takes in two arguments - the string and the number of characters you want to generate.

3. How can I generate random characters from a string in JavaScript?

In JavaScript, you can use the Math.random() function to generate a random number and then use the charAt() method to get a character from a string at a specific index. You can also use a similar approach as in Python by using a for loop to iterate through the string and generate multiple random characters.

4. Can I generate random characters from a string in Java?

Yes, in Java you can use the Random class to generate random numbers and then convert them to characters using the char data type. You can also use the charAt() method to get a character from a string at a specific index.

5. How can I generate random characters from a string in C++?

In C++, you can use the rand() function to generate a random number and then use the string::operator[] method to get a character from a string at a specific index. You can also use a similar approach as in Python and JavaScript by using a for loop to iterate through the string and generate multiple random characters.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
933
  • Programming and Computer Science
Replies
1
Views
732
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
2
Replies
37
Views
4K
Back
Top