I need help with rand()

Discussion in '3DS - Homebrew Development and Emulators' started by cynosura, Jan 27, 2016.

  1. cynosura
    OP

    cynosura GBAtemp Regular

    Member
    152
    52
    Jun 7, 2015
    France
    Bed
    Hi, I'm using the rand() function to determine random x and y positions (I'm working on a project with sf2dlib).
    But X and Y are always the same.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <3ds.h>
    #include <sf2d.h>
    
    int main(){
       srand(time(NULL));
    
       xC = rand() % 320;
       yC = rand() % 240;
    }
    the code is very simplified. Can anyone explain me why I have this issue ? thanks.
     
  2. Rinnegatamante

    Rinnegatamante GBAtemp Psycho!

    Member
    3,127
    3,267
    Nov 24, 2014
    Italy
    Bologna
    Try to change time(NULL) with osGetTime() and see if it change something (don't know if time is already available in ctrulib.)
     
  3. Coto

    Coto GBAtemp Addict

    Member
    2,365
    415
    Jun 4, 2010
    Chile
  4. Slashcash

    Slashcash GBAtemp Fan

    Member
    334
    461
    Oct 15, 2015
    Italy
    It seems perfectly fine to me. Or the time() function is broken on 3ds (it would surprise me since it's from the standard library) or the problem is somewhere else in the code.
     
  5. cynosura
    OP

    cynosura GBAtemp Regular

    Member
    152
    52
    Jun 7, 2015
    France
    Bed
    Okay thanks !
    osGetTime() is a different seed, but always give the same numbers (different from time(NULL) of course)...
    Thanks for the information.

    — Posts automatically merged - Please don't double post! —

    I hope no
     
    Last edited by cynosura, Jan 27, 2016
  6. Slashcash

    Slashcash GBAtemp Fan

    Member
    334
    461
    Oct 15, 2015
    Italy
    Wait! Are you trying to get different random values for x and y? Because the code you wrote obviuosly gives the same x and y values.

    If you want to get different values you have to do:

    Code:
    srand(time())
    
    x = rand()
    
    srand(time())
    
    y = rand()
    (obviuosly pseudocode)
     
  7. cynosura
    OP

    cynosura GBAtemp Regular

    Member
    152
    52
    Jun 7, 2015
    France
    Bed
    x and y values are differents, it's just always the sames (they are differents, but if I restart the homebrew, it will be the same set of values...)
     
  8. Slashcash

    Slashcash GBAtemp Fan

    Member
    334
    461
    Oct 15, 2015
    Italy
    Sorry, my bad, my code was stupid. Effectively the code seems fine. Have you tried to print the seed returned by time() ? If you get a different seed everytime but rand() always give you the same sequence there is something wrong with rand() on 3ds (highly unlikely) or...somewhere else in your code? This is really strange.
     
    cynosura likes this.
  9. Gocario

    Gocario GBAFail'd

    Member
    640
    560
    Sep 5, 2015
    France
    Bourg Palette
    I tested that, and it worked well.

    About your program, does it run well on Citra?
    If so maybe your 3ds clock has a problem.