Rand() function


#1

Inside the +(id)randomItem class method we used the rand() function. I looked at the rand() documentation in Xcode and it said that it had been deprecated in favor of random() because rand() produces an inferior random sequence. Is there any reason to use random() instead of rand() in our future apps that implement randomness? Or is the difference too small to worry about? Thanks.


#2

It doesn’t really matter, but you can read up on the basics of the subject here: http://en.wikipedia.org/wiki/Random_number_generation

Mersenne Twister is the RNG used by many games these days. And by these days, I mean last time I checked… they move so fast in that industry.


#3

Hello everyone, this exercise something happens to me very curious, I tell you. When I run the project, the data that paints the log appear random, but no matter how many times you run that always come out the same data “random”.

Do you think that may be because they deprecated method?

Sorry for my English :slight_smile:

Thanks and best regards


#4

Most random number generators use a seed number to begin with. This seed number dictates the pattern of all the numbers to be generated. Since we never seed the generator, its seed defaults to 0. Thus, each time you run the app, the seed is 0, and generates the same pattern of numbers. You can seed the generator with a “unique” value each time the app launches by doing this at the beginning of main:

srand(time(NULL));

This uses the current time (in seconds) to seed the generator. Of course, if you run the app twice within a second, you’ll get the same results.


#5

Thanks for your help Joe!


#6

[quote=“JoeConway”]It doesn’t really matter, but you can read up on the basics of the subject here: http://en.wikipedia.org/wiki/Random_number_generation

Mersenne Twister is the RNG used by many games these days. And by these days, I mean last time I checked… they move so fast in that industry.[/quote]

Mersenne Twister is pretty much the RNG of choice as far as I’m concerned. I use it in my work (which requires Monte Carlo simulations of billions of trials) and it is extremely solid. Plus, versions can be found in pretty much every C family dialect, using both 32 and 64 bit architecture.


#7

[quote=“blunk33”]Hello everyone, this exercise something happens to me very curious, I tell you. When I run the project, the data that paints the log appear random, but no matter how many times you run that always come out the same data “random”.
[/quote]

I had the same “problem” when I executed my code and the same question popped up for me.

Adding that srand(time(NULL)); did give me random results every time there after. Great question and thanks to everyone who responded!