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
9
votes
2 answers

How do I generate a random number not including one without using a while loop?

Let's say I want to generate a random number between 1 and 100, but I don't want to include 42. How would I do this without repeating the random method until it is not 42.
dhruvm
  • 2,672
  • 2
  • 19
  • 24
8
votes
3 answers

random BOOLs in an efficient way for cocos2d

According to Steffen's post this is an efficient way to generate random BOOLs in cocos2d +(BOOL) getYesOrNo { return (CCRANDOM_0_1() < 0.5f); } but how do I set a range for this? (e.g. 0 - 29 is the interval and 5 ones BOOL = NO, 25 ones BOOL =…
el.severo
  • 2,202
  • 6
  • 31
  • 62
7
votes
2 answers

Checking if Firebase snapshot is equal to nil in Swift

I am trying to query Firebase to check if any user that has waiting: "1" and then when the snapshot is returned I want to see whether it is equal to nil. I have attempted to do this but the method I have used does not work and I only have some sort…
Tom Fox
  • 897
  • 3
  • 14
  • 34
7
votes
3 answers

Cannot convert value of type 'Int' to expected argument type 'UInt32'

I am trying to generate a random number in Swift: var amountOfQuestions = 2 var randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1 but this results in the error: Cannot convert value of type 'Int' to expected argument type…
Tom Fox
  • 897
  • 3
  • 14
  • 34
7
votes
1 answer

Swift expression was too complex to be solved in reasonable time

I'm having an error when compiling a project in Xcode, it says: Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions here's the code: static func random(min: CGFloat, max:…
lagro23
  • 123
  • 1
  • 6
7
votes
1 answer

OS requirements for arc4random_uniform()

How do I find out the minimum OS requirements for using arc4random_uniform()? Is it defined in BSD? If so, from what version? Does it run on any Mac OS X version? How about iOS versions? Is there any official place I can find these things out?
rid
  • 61,078
  • 31
  • 152
  • 193
6
votes
2 answers

I fear that arc4random has betrayed me

I have code that pics a random number from 0 to 1. I am seeing that the number 1 is coming up far more times then the number 0 then I would think to be statistically possible. This is my code: int shipNumber = arc4random() % 2; Should this code…
Mel
  • 2,055
  • 2
  • 26
  • 45
6
votes
1 answer

arc4random_uniform not available in Xcode 7.0 beta (7a176x) on OSX 10.10.4

I'm trying to use arc4random_uniform in the Xcode build mentioned, but it seems to no longer be available: An alt-click on the available functions show that they're declared in stdlib.h, which has them listed as follows: It seems strange that its…
Kyle G
  • 4,347
  • 4
  • 26
  • 39
5
votes
2 answers

Is there anything wrong with this RC4 encryption code in C#

I am trying to listen to the Foxycart XML Datafeed in C# and running into an issue which boils down to encryption. In short, they send over their data as encoded and encrypted XML using RC4 encryption. To test, they have some (user submitted)…
leora
  • 188,729
  • 360
  • 878
  • 1,366
5
votes
1 answer

Why is rand() much faster than arc4random()?

I'm writing a game AI which requires fast integer random number generation. This game is for Mac OS, so there are two choices rand() (the plain C) and arc4random() (BSD). I didn't find any comparison in speed for these two functions, so I wrote a…
Zhigang An
  • 296
  • 2
  • 13
5
votes
1 answer

Eliminating modulo bias: how is it achieved in the arc4random_uniform() function?

Modulo bias is a problem that arises when naively using the modulo operation to get pseudorandom numbers smaller than a given "upper bound". Therefore as a C programmer I am using a modified version of the arc4random_uniform() function to generate…
Log
  • 239
  • 1
  • 5
5
votes
2 answers

Generate Random Numbers with Fixed Digits Length?

I am generating Random Number with int randomID = arc4random() % 3000; But I want to generate random number with atleast 4 digits. like 1000, 2400, 1122 I want to know the code for Objective C.
Amir iDev
  • 1,257
  • 2
  • 15
  • 29
5
votes
1 answer

How to generate random 6 digits number by a button?

I tried random generate number from 1 to 100.How to change random 6 digits ? Note :Numbers can not start with 0(zero) Random from 1 to 100 codes #import @interface RandomNumberGenViewController : UIViewController { int number; …
Mhmt
  • 759
  • 3
  • 22
  • 45
4
votes
1 answer

arc4random() range including negatives

Looking to find how I would format a call to arc4Random() to use a range of numbers from -10 to 10. Or does arc4Random() only generate from 0 to X? If that's the case I will need to manipulate the result from arc4Random() so that it may be a result…
Demasterpl
  • 2,103
  • 5
  • 24
  • 32
4
votes
3 answers

arc4random Random Number Generator

int randomNumber = (arc4random() % 83) + 1; Is this the best way to generate "the most random" number? Or is there a better way to generate a random number?
Linuxmint
  • 4,716
  • 11
  • 44
  • 64
1
2
3
20 21