Very short snippet: iPhone, random numbers

Friday, October 9th, 2009

If you need to generate random numbers in an application from the Apple iPhone you have to put Objective-C, because it proposes no class for this purpose. The solution is that proposed by the C: rand(), srand(), random(), srandom() e arc4random()
Of all the most popular is definitely arc4random() because it does not require any special initialization or seeding functions, returning a good value "random", we could say that it is self-inizializzante.

Objective-C:
  1. / / Generate a random number from 0 to 99
  2. arc4random ( ) % 100 ; int rnd = arc4random ()% 100;
  3. / / Generate a random number from 20 to 50
  4. arc4random ( ) % 51 ) + 20 ; int rnd = (arc4random ()% 51) + 20;

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

There are no comments for this post

Leave a comment

TAG XHTML PERMISSIONS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre>         // blocco generico [code][/code]       // blocco generico [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL