Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 384 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating random numbers in Objective-C

#11
As of iOS 9 and OS X 10.11, you can use the new GameplayKit classes to generate random numbers in a variety of ways.

You have four source types to choose from: a general random source (unnamed, down to the system to choose what it does), linear congruential, ARC4 and Mersenne Twister. These can generate random ints, floats and bools.

At the simplest level, you can generate a random number from the system's built-in random source like this:

NSInteger rand = [[GKRandomSource sharedRandom] nextInt];

That generates a number between -2,147,483,648 and 2,147,483,647. If you want a number between 0 and an upper bound (exclusive) you'd use this:

NSInteger rand6 = [[GKRandomSource sharedRandom] nextIntWithUpperBound:6];

GameplayKit has some convenience constructors built in to work with dice. For example, you can roll a six-sided die like this:

GKRandomDistribution *d6 = [GKRandomDistribution d6];
[d6 nextInt];

Plus you can shape the random distribution by using things like `GKShuffledDistribution`.
Reply

#12
Same as C, you would do

#include <time.h>
#include <stdlib.h>
...
srand(time(NULL));
int r = rand() % 74;

(assuming you meant including 0 but excluding 74, which is what your Java example does)

**Edit:** Feel free to substitute `random()` or `arc4random()` for `rand()` (which is, as others have pointed out, quite sucky).
Reply

#13
For game dev use random() to generate randoms. Probably at least 5x faster than using arc4random(). Modulo bias is not an issue, especially for games, when generating randoms using the full range of random(). Be sure to seed first. Call srandomdev() in AppDelegate. Here's some helper functions:

static inline int random_range(int low, int high){ return (random()%(high-low+1))+low;}
static inline CGFloat frandom(){ return (CGFloat)random()/UINT32_C(0x7FFFFFFF);}
static inline CGFloat frandom_range(CGFloat low, CGFloat high){ return (high-low)*frandom()+low;}



Reply

#14
You should use the `arc4random_uniform()` function. It uses a superior algorithm to `rand`. You don't even need to set a seed.

#include <stdlib.h>
// ...
// ...
int r = arc4random_uniform(74);


The `arc4random` man page:



> NAME
> arc4random, arc4random_stir, arc4random_addrandom -- arc4 random number generator
>
> LIBRARY
> Standard C Library (libc, -lc)
>
> SYNOPSIS
> #include <stdlib.h>
>
> u_int32_t
> arc4random(void);
>
> void
> arc4random_stir(void);
>
> void
> arc4random_addrandom(unsigned char *dat, int datlen);
>
> DESCRIPTION
> The arc4random() function uses the key stream generator employed by the arc4 cipher, which uses 8*8 8
> bit S-Boxes. The S-Boxes can be in about (2**1700) states. The arc4random() function returns pseudo-
> random numbers in the range of 0 to (2**32)-1, and therefore has twice the range of rand(3) and
> random(3).
>
> The arc4random_stir() function reads data from /dev/urandom and uses it to permute the S-Boxes via
> arc4random_addrandom().
>
> There is no need to call arc4random_stir() before using arc4random(), since arc4random() automatically
> initializes itself.
>
> EXAMPLES
> The following produces a drop-in replacement for the traditional rand() and random() functions using
> arc4random():
>
> #define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through