Questions tagged [arc4random]

The arc4random() function returns pseudo-random numbers in the range of 0 to (2*32)-1

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() and random().

For example, a drop-in replacement for the traditional rand() and random() functions using arc4random():

#define foo4random() (arc4random() % ((unsigned)RAND_MAX + 1))
305 questions
133
votes
18 answers

How does one make random number between range for arc4random_uniform()?

so my goal in this codebit is to randomly roll two dice and as we all know your regular die only has 6 sides so I imported Foundation for access to arc4random_uniform(UInt32). I attempted using the range of (1..7) to avoid randomly getting 0 however…
arcreigh
  • 1,423
  • 2
  • 11
  • 12
95
votes
16 answers

Generating random numbers with Swift

I need to generate a random number. It appears the arc4random function no longer exists as well as the arc4random_uniform function. The options I have are arc4random_stir(), arc4random_buf(UnsafeMutablePointer, Int), and …
Big_Mac
  • 2,975
  • 4
  • 20
  • 36
88
votes
11 answers

Swift random float between 0 and 1

In Swift, I'm trying to get a random float between 0 and 1 but I can't seem to get the type conversions to work. func randomCGFloat() -> CGFloat { return CGFloat(arc4random()) / UINT32_MAX } I'm getting a 'CGFloat' is not convertible to 'UInt8'…
Joe_Schmoe
  • 1,485
  • 3
  • 17
  • 19
84
votes
12 answers

Generate a random float between 0 and 1

I'm trying to generate a random number that's between 0 and 1. I keep reading about arc4random(), but there isn't any information about getting a float from it. How do I do this?
Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
53
votes
6 answers

Generate Random Numbers Between Two Numbers in Objective-C

I have two text boxes and user can input 2 positive integers (Using Objective-C). The goal is to return a random value between the two numbers. I've used "man arc4random" and still can't quite wrap my head around it. I've came up with some code but…
Demasterpl
  • 2,103
  • 5
  • 24
  • 32
52
votes
1 answer

What's the difference between arc4random and arc4random_uniform?

I've seen old posts about the differences between random and arc4random in Objective-C, and I've seen answers to this online but I didn't really understand, so I was hoping someone here could explain it in an easier-to-understand manner. What is the…
Brennan Adler
  • 895
  • 2
  • 8
  • 18
42
votes
6 answers

How to select range of values when using arc4random()

Can I set a range of numbers when using arc4random()? For example 50-100 only.
NextRev
  • 1,711
  • 4
  • 22
  • 31
26
votes
5 answers

seeding arc4random() in iOS

From what I can gather arc4random() generates much better random numbers than rand() does, however I haven't seen a way to seed it, and I would like to just like using srand(). Is there a way?
andrewz
  • 4,729
  • 5
  • 49
  • 67
19
votes
5 answers

Create an array of random numbers in Swift

I'm just starting to learn Swift. I'm attempting to create an array of several random numbers, and eventually sort the array. I'm able to create an array of one random number, but what's the best way to iterate this to create an array of several…
eloist
  • 465
  • 3
  • 10
  • 22
15
votes
1 answer

How random is arc4random (mac os x)? (or what am I doing wrong?)

I'm playing with an optimized game of life implementation in swift/mac_os_x. First step: randomize a big grid of cells (50% alive). code: for(var i=0;i<768;i++){ for(var j=0;j<768;j++){ let r = Int(arc4random_uniform(100)) let alive =…
pmeyer
  • 643
  • 1
  • 5
  • 10
14
votes
7 answers

Get random object from Array

I want to get random object from array, is there any way how can I find random object from mutable array?
Sonu
  • 937
  • 1
  • 10
  • 39
14
votes
5 answers

Swift: Random number for 64-bit integers?

So, with my current project, I need to work with 64-bit integers and I need to grab random numbers between ranges up to 100 billion. arc4random()/arc4random_uniform() only works with unsigned 32-bit integers. I can probably fudge it a little because…
RH224
  • 141
  • 1
  • 5
13
votes
2 answers

Instance member cannot be used on type 'ViewController'

class ViewController: UIViewController { let fortuneArray = ["You will find true love in the summer.", "Outlook not good", "You may find great success in business soon.", "Watch out for grey cats."] let randomIndex =…
Nicholas L.
  • 293
  • 2
  • 3
  • 9
12
votes
1 answer

Arc4random modulo biased

According to this documentation, arc4random_uniform() is recommended over constructions like arc4random() % upper_bound as it avoids "modulo bias" when the upper bound is not a power of two. How bad is the bias? For example if I generate random…
AJ222
  • 1,114
  • 2
  • 18
  • 40
10
votes
6 answers

iOS: How do I generate 8 unique random integers?

I need to generate 8 random integers, but they need to be unique, aka not repeated. For example, I want 8 numbers within the range 1 to 8. I've seen arc4random but I'm not sure how to make them unique ? Solution -(NSMutableArray…
Jules
  • 7,568
  • 14
  • 102
  • 186
1
2 3
20 21