Runtime Random Generators, not Compile Time [duplicate] - objective-c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
iPhone: random() function gives me the same random number everytime
I am writing a test iPhone app for a larger project that I am working on that will involve randomized strings, numbers, etc. When I use the rand() or random() functions, every single time I do get randomized numbers and strings, but in the same order! I know that the compiler determines the order at compile time, but do not want that. I want it to be completely random, so something different every time not just a predetermined list. What I have tried is a loop that counts up and down to try to take a value from that but it didn't work.

The compiler doesn't generate the random strings; they're generated at runtime. But they are generated based on an initial seed; for a given value of the seed, you'll get the same sequence of numbers. You need to choose a seed at runtime based on something like the system time, uptime, count of user clicks, etc.

You need to seed the number for rand. This way it will be random every time!
Here is an example of what I mean:
// Seed number for rand()
srand((unsigned int) time(0) + getpid());
Add the above line of code before using rand()

Random number generators are never truly "random," and instead generate a number based on something that should always be different. Giving a random number generator a unique value so that it can generate a random number is know as "seeding." For rand(), you will want to seed it with srand(time(NULL)). For random() it's the same deal with srandom(). There are functions available for iOS such as arc4random(), which is self-seeding. To generate a random number up to (but not including) 10, for instance, you could use arc4random() as follows:
int random = arc4random() % 10;

Related

What is the purpose of seed in Sql function RAND()?

I am aware that Rand() is used to generate a random number less than 1 and greater than 0.
if you supply seed , it will generate same value for same seed in the same connection everytime you execute it.
Also select RAND(1),RAND() both would always give you same resultset everytime you execute it in same connection.
But how is seed useful?
It's a bit implicit, but is well-documented here:
Repetitive calls of RAND() with the same seed value return the same results.
For one connection, if RAND() is called with a specified seed value, all subsequent calls of RAND() produce results based on the seeded RAND() call. For example, the following query will always return the same sequence of numbers.
SELECT RAND(100), RAND(), RAND()
So, SELECT RAND(1), RAND() will return the same numbers and SELECT RAND(), RAND() won't.
A set of numbers which have been randomly generated once, but which remains the same over each invocation, is useful in cases where you need to share this specific set. In other words, the seed maps deterministically to a set of normally-distributed numbers; if all the users of the seed are using the same PRNG, they will receive the same (infinite) set of pseudorandom numbers without the need for further synchronization.
An example use case: "this is a randomly shuffled deck of cards, see how well your AIs can play it - in repeated matches with the same deck."
Or "for this Minecraft competition, you will use the world generated with seed #123456789, and do something awesome with it."
See also this: http://rednuht.org/genetic_cars_2/ there's a "seed" field, which will generate the same set of tracks, which is useful for testing various populations on the same terrain. Here, you can see the effect of a shared seed interactively.

What is a good Random function to use that will be different every time it starts

I am using rand() function, but it always uses the same random sequence. Is there a random function that seeds with the clock value? And how would I do this?
rand() requires you to specify the seed. The best way to specify a seed is to use the current time.
// specify the seed
srand(time(NULL));
Or you can use arc4random.
You are meant to seed rand() and random() (slightly bigger space) yourself, with their respective seed functions, before using them. You can use the time, or whatever other value you desire:
srand(time(0));
srandom(time(0));
Here we get the system time; obviously passing a constant will produce the same sequence every run.
You can also use arc4random() which generates very high-quality random bits and seeds itself using /dev/random.

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.

SQL Server newID - how is it created?

I would like to use newId to generate random numbers. Usually you would use it just once, but I might be generating up to 10 random numbers per newId.
Is it random enough?
Usually you would use it just once, but I might be generating up to 10 random numbers per newId. Is it random enough?
It depends on how you extract the numbers from the newid. You cannot treat it as 128 independently random bits.
For example if you use the first 8 bits for generating one random number between 0 and 255, use the next 8 bits to generate another number, etc. then you will see that your numbers will not be uniformly random.
v
E058D654-35A8-47F2-AE40-1C4EEBBDC549
01461481-ED8D-4B85-90FA-C08621D98DAE
AE861E4E-3469-4BDB-A38B-0031DACC8DAE
AF8905D0-E41B-4300-94F2-33BB45698CD1
003308A6-AE0A-4E20-9F24-047A6955E748
76F9B7ED-79AB-4EB1-B361-8C0AF5177CE3
B8F1CAC0-591D-436B-BB21-FAAD9EECA983
7FBEAEFD-2163-4315-A783-8106909E47D8
85E2FC60-E7B3-400F-B20A-CEFBECAEE4F9
17ED0A03-ADAD-4521-97EE-04815A867B32
^
|
always 4
You should also try to avoid reusing the same bits to generate different random numbers as your numbers will become related. If in doubt, don't reuse the same number.
Note that there is also a RAND function which you can call. This returns numbers from a uniform distribution.
Yes, it's statistically random. It's simply a GUID.
How do you plan on generating 10 numbers from one seed though? CHECKSUM(NEWID()) is normally how you'd do it for one value, perhaps with modulo and ABS
NewID generates a GUID. It is random enough.
Random enough for what? When you say use it to generate, are you just going to use it to seed a PNRG? I'm not sure it's any better than a timestamp for that. Or are you going to extract bits from the GUID - that's a bad idea.
http://www.random.org/randomness/

Generating Random Numbers in Objective C for iPhone SDK

I was using the arc4random() function in order to generate a random group and sequence of numbers, but I was told that this was overkill and that I should use the random() function instead. However, the random() function gives me the same group and sequence of numbers every time.
I call srand(time(0)) once when my app first starts in order to seed the random() function. Do you ever need to reseed the random() function?
Am I missing something?
Thanks.
First off, who told you arc4random was overkill? I use it in my projects, and it (a) satisfies my requirements, (b) doesn't suck down resources (at least any visible to the user or obvious to me), and (c) was trivial to implement, so I don't really see how a similar use in your own code could be called "overkill."
Second, srand() seeds the rand() function, not random(), so that may be your issue. And no, you shouldn't have to reseed the generator at any time during your program's execution - once during startup is enough.
No, you do not need to reseed the random number generator. There is some additional uniformity gained by generating some amount of numbers and throwing them away, but unless you are looking for security level random number generation there is no need. For most purposes a properly seeded random number generator is uniform enough.