![]() |
| ||
| Random Choice 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? |
| ||
| Re: Random Choice rand() returns a random value between 0 and MAX_RAND. |
| ||
| Re: Random Choice |
| ||
| Re: Random Choice 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. |
| ||
| Re: Random Choice Of course. I already mentioned you should use the rand() function. Lets say the string is "Hello World" char str[] = "Hello World"; The above is a little long winded, but illustrates how to do it. |
| ||
| Re: Random Choice Oh okay. Thanks a lot. |
| All times are GMT -4. The time now is 7:30 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC