Skip to contentSkip to conversation

Randomizing

DISCUSSION Progga 2 participants 2 messages Apr 12, 2003

lore
Share & tools
lore

Progga

author
#0 lore raw

Is there any function for randomizing a string (like glibc's strfry() ) which
can be used across different Unices ?

Khoda Hafez
Progga

#1 lore raw
Quoted 2 lines · Progga

Is there any function for randomizing a string (like glibc's strfry() ) which can be used across different Unices ?

No; but it's not hard to write one yourself:

	void randomize(char *str)
	{
		int len = strlen(str);
		int i;
		for (i = 0; i < len; i++)
		{
			int n = i + rand() % (len - i);
			char tmp = str[i];
			str[i] = str[n];
			str[n] = tmp;
		}
	}
Signature · 1 line
Glynn Clements <[email protected]>