Hi I'm pretty new to C (about a week experience) and I was wondering if C had anything like Python's random.choice() function. Basically it just selected a random element from what you gave it, a number from an array or a character from a string. If there is no function for this, if anyone could suggest a way around it, so for instance, I have a string "Hello World", how could I pull a random character out of that?
Erikmmp 0 Newbie Poster
Recommended Answers
Jump to Postrand() returns a random value between 0 and MAX_RAND.
Jump to PostOf course. I already mentioned you should use the rand() function. Lets say the string is "Hello World"
char str[] = "Hello World"; int len = strlen(str); // length of the string int x = rand() % len; // get a number between 0 and length of …
All 5 Replies
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
devnar 114 Junior Poster
Erikmmp 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Erikmmp 0 Newbie Poster
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.