Random Choice

Reply

Join Date: Nov 2008
Posts: 11
Reputation: Erikmmp is an unknown quantity at this point 
Solved Threads: 0
Erikmmp's Avatar
Erikmmp Erikmmp is offline Offline
Newbie Poster

Random Choice

 
0
  #1
Nov 30th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,141
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Random Choice

 
0
  #2
Nov 30th, 2008
rand() returns a random value between 0 and MAX_RAND.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Random Choice

 
0
  #3
Nov 30th, 2008
More info here and here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: Erikmmp is an unknown quantity at this point 
Solved Threads: 0
Erikmmp's Avatar
Erikmmp Erikmmp is offline Offline
Newbie Poster

Re: Random Choice

 
0
  #4
Dec 1st, 2008
What I mean is, is there any way I could select a character from a string at random? for instance in python:
x = random.choice("Hello World")
and x would return of value of say "W" as an example.
Anyway at all to do this?
Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,141
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Random Choice

 
0
  #5
Dec 1st, 2008
Of course. I already mentioned you should use the rand() function. Lets say the string is "Hello World"
  1. char str[] = "Hello World";
  2. int len = strlen(str); // length of the string
  3. int x = rand() % len; // get a number between 0 and length of string
  4. char c = str[x]; // get the character

The above is a little long winded, but illustrates how to do it.
Last edited by Ancient Dragon; Dec 1st, 2008 at 7:06 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: Erikmmp is an unknown quantity at this point 
Solved Threads: 0
Erikmmp's Avatar
Erikmmp Erikmmp is offline Offline
Newbie Poster

Re: Random Choice

 
0
  #6
Dec 1st, 2008
Oh okay. Thanks a lot.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC