Why arc4random_uniform instead of random or rand? [duplicate] - objective-c

This question already has answers here:
Arc4random modulo biased
(1 answer)
What's the difference between arc4random and random?
(2 answers)
Closed 8 years ago.
I've been trying to create program in Objective-C to return random value between specific minimum to maximum value.
I've searched online and found out people are using arc4random_uniform instead of random() or rand().
My question is how arc4random is different from random()?
random() is definitely easier to remember and I get same result from both arc4random and random()
Thank You.
Regards.

Related

Why do I get different result on Xcode (C Language) [duplicate]

This question already has answers here:
Why does sizeof(int) vary across different operating systems?
(4 answers)
Closed 3 years ago.
long number = 100;
printf("the sizeof: %d", sizeof(number));
In Visual Studio, I get the result 4.
In Xcode, whereas I get the result 8.
Same code, different result. Could you tell me why I get this result?
See here: long is guaranteed by the standard to be "at least 32 bits", and depending on the data model may be bigger.
If you want "exactly 32 bits", try if int32_t is supported.

Is there a Objective-C or C math function to constrain a variable between a Min and a Max? [duplicate]

This question already has answers here:
Fastest way to clamp a real (fixed/floating point) value?
(14 answers)
Closed 6 years ago.
What I am looking for is a math function that constrains a primitive variable between a minimum and a maximum value in only one single function call, if it exists, in the standard math libraries for Objective-C.
I currently use:
float constrainedValue = fminf( maxValue, fmaxf( minValue, inValue ) );
Since I know that both fminf and fmaxf could potentially have instruction jumps or branches, it seems likely there could be a simple routine that could conjoin both of these operations into one, optimized function.
This topic is thoroughly discussed here: Fastest way to clamp a real (fixed/floating point) value?
'clamp' is the keyword I was looking for.

How long can a string be in Objective-C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the maximum length of an NSString object?
I'm trying to decide on a good way to store my tile data on the parse database. What I need is to store 24x24 values, not sure how long those values will be, but let's say I need to store 24x24 bytes (giving me 256 possibilities for each tile). I was thinking of storing them in a big string, is that possible? is there a limit on how big a string should be? or is there a better way to do this in objective c?
Thanks for any advice.
Re: string length, I don't think there's a hard limit on the length of a string. But in terms of storing this type of data, how about a multi-dimensional array? Here's a discussion on the topic.
How to declare a two dimensional array of string type in Objective-C?
The maximum length is determined by the size of NSUInteger (so, billions).
This question has already been asked and answered on stackoverflow here
What is the maximum length of an NSString object?

Data Types Obj-C [duplicate]

This question already has an answer here:
Closed 11 years ago.
Exact Duplicate:
Issue with float and double data types in objective C
[Ironically, to find the duplicate questions you need to know the answer.]
What Every Computer Scientist Should Know About Floating-Point Arithmetic
If it cannot be expressed in base 2, it will not be precise. See also floating point inaccuracy.
0.1 is a 'repeating decimal' in binary (0.0001100110011...) so the representation of 0.1 is inexact. NSLog is likely rounding or truncating the output.

same random number should not generate again and again? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
C++ random number generator without repeating numbers
Unique random numbers in O(1)?
I am doing as following
int y = arc4random() % 50;
I am using objective-c
Now as I don't want same number again and again, like if 6 is once I get, then I don't need 6 again, because I am calling this line again and again and taking random numbers.
How about you generate an array (1,2,3,4,5,6,...) and sort it randomly, then read the array elements one at a time.
Otherwise, if you use a random number generator and make sure it only gives you numbers you haven't seen before, you can only call it a limited number of times.
sorting randomly will depend on what language you're using.