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
4
votes
3 answers

Could not cast value of type 'Swift.UInt32' to 'Swift.Int'

In Tests project I've got extensions with some test helper functions. Like this: extension Employee { static func mockDict() -> Dictionary! { return ["ID": arc4random() % 1000, "FirstName": "Employee First…
user1232690
  • 481
  • 5
  • 16
4
votes
3 answers

Generic Random Function in Swift

I have researched and looked into many different random function, but the downside to them is that they only work for one data type. I want a way to have one function work for a Double, Floats, Int, CGFloat, etc. I know I could just convert it to…
grahamcracker1234
  • 591
  • 1
  • 7
  • 27
4
votes
4 answers

Better random "feeling" integer generator for short sequences

I'm trying to figure out a way to create random numbers that "feel" random over short sequences. This is for a quiz game, where there are four possible choices, and the software needs to pick one of the four spots in which to put the correct answer…
John Biesnecker
  • 3,782
  • 2
  • 34
  • 43
4
votes
3 answers

Create double by appending binary representations of two ints

I'm trying to create a double by appending the binary representations of two ints. This is what I have now: int i = arc4random(); *(&i+1) = arc4random(); double d = *(double*)&i; I hoped the double pointer would also use the value in the &i+1…
Rugen Heidbuchel
  • 377
  • 2
  • 14
4
votes
2 answers

arc4random non-modulo bias?

I am using arc4random() %2 in my code. It is called by 3 classes upon initialization, which happens in quick succession. However, approximately 70% plus of the results are always either all 0 or all 1 (the distribution between the 2 sets of 000 or…
RunLoop
  • 20,288
  • 21
  • 96
  • 151
4
votes
4 answers

Non-repeating arc4random_uniform

I've been trying to get non-repeating arc4random_uniform to work for ages now for my iPhone app. Been over all the questions and answers relating to this on stackoverflow with no luck and now I'm hoping someone can help me. What I want to do is is…
Jens Hendar
  • 174
  • 1
  • 2
  • 10
3
votes
1 answer

arc4random and % operator

I have a question about the arc4random() function in Objective-C. In the examples I have seen online, there is a % symbol right after the function call. I think of % as the modulus operator, so does this symbol have another meaning when used after…
Tony
  • 81
  • 1
  • 3
3
votes
1 answer

Awkward arc4random results

I'm using this code, where 'length' value is '50'. newX = (arc4random()%(lenght+1)) - (lenght/2); newY = (arc4random()%(lenght+1)) - (lenght/2); NSLog(@"Creature Move X:%f, Y:%f", newX, newY); But in the debugger I get things like: 2012-01-02…
David Da Silva Contín
  • 1,051
  • 2
  • 10
  • 20
3
votes
2 answers

How to add a variable's integer to another variable's integer

I'm trying to add randomAmountOfTime to DispatchTime.now() but it gives me an error. Error: Binary operator '+' cannot be applied to operands of type 'DispatchTime' and 'Int' Here's what I did: var randomAmountOfTime =…
Claire Cho
  • 87
  • 1
  • 11
3
votes
1 answer

shortest code to create an array of random numbers in swift?

I want to create an array of random numbers (Int, Int32) I tried the following: map(1...1000) { arc4random() } but it returns the following error: error: type 'ClosedInterval' does not conform to protocol 'SequenceType' What I'm doing…
marco alves
  • 1,707
  • 2
  • 18
  • 28
3
votes
1 answer

Swift array access triggers EXC_BREAKPOINT

Here is my code (largeAsteroids.count is never 0): var largeAsteroids=[[SKTexture]]() func randomLargeAsteroidTextures()->Array{ let i=Int(arc4random())%largeAsteroids.count return largeAsteroids[i]// this line triggers…
Yongxu Ren
  • 111
  • 6
3
votes
2 answers

Calling arc4random several times and getting the same array set

I need a method to generate 4 numbers positioned randonly in an array. This method must be able to be called several times. The code that I tried below seems to be working.. except that everytime I call it, it generates the very same numbers…
RickON
  • 395
  • 7
  • 18
3
votes
3 answers

Why am I getting strange garbage with NSInteger and arc4random?

I'm trying to get a random number using arc4random between -60 and 60. I generate avnumber between 1 and 120 inclusively, and then subtract 60. If this is in one line, I get weird results whenever the number is negative. //Loop illustrates point…
Eliza Wilson
  • 1,031
  • 1
  • 13
  • 38
3
votes
2 answers

Generate random number from given number

I am stuck in one issue regarding to random number. I am generating random number using arc4random(). I want to generate randome numbers between 0-9 at first. so i am using: int RandomNumber = arc4random() % 9; its good. Now suppose I got 8. I…
Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
3
votes
1 answer

arc4random initialisation

I am using random number generation as part of a procedure for minimising a function (using the Nelder-Mead simplex algorithm) in objective-c (for iOS). I have used arc4random() because it seems to be recommended everywhere on the grounds that a)…
Rob Bullen
  • 75
  • 4
1 2
3
20 21