Generating random numbers in Objective-C

前端 未结 13 2261
孤街浪徒
孤街浪徒 2020-11-22 02:07

I\'m a Java head mainly, and I want a way to generate a pseudo-random number between 0 and 74. In Java I would use the method:

Random.nextInt(74)
         


        
13条回答
  •  逝去的感伤
    2020-11-22 02:20

    Same as C, you would do

    #include 
    #include 
    ...
    srand(time(NULL));
    int r = rand() % 74;
    

    (assuming you meant including 0 but excluding 74, which is what your Java example does)

    Edit: Feel free to substitute random() or arc4random() for rand() (which is, as others have pointed out, quite sucky).

提交回复
热议问题