Homebrew I need help with rand()

randomdev

Well-Known Member
OP
Member
Joined
Jun 7, 2015
Messages
155
Trophies
0
XP
236
Country
Brazil
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.
 

Rinnegatamante

Well-Known Member
Member
Joined
Nov 24, 2014
Messages
3,162
Trophies
2
Age
29
Location
Bologna
Website
rinnegatamante.it
XP
4,857
Country
Italy
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.

Try to change time(NULL) with osGetTime() and see if it change something (don't know if time is already available in ctrulib.)
 

Slashcash

Well-Known Member
Member
Joined
Oct 15, 2015
Messages
338
Trophies
0
XP
611
Country
Italy
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.

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.
 

randomdev

Well-Known Member
OP
Member
Joined
Jun 7, 2015
Messages
155
Trophies
0
XP
236
Country
Brazil
Okay thanks !
Try to change time(NULL) with osGetTime() and see if it change something (don't know if time is already available in ctrulib.)
osGetTime() is a different seed, but always give the same numbers (different from time(NULL) of course)...
well:

http://www.tutorialspoint.com/c_standard_library/c_function_srand.htm

the seed you give to srand is static in that example
Thanks for the information.

--------------------- MERGED ---------------------------

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.
I hope no
 
Last edited by randomdev,

Slashcash

Well-Known Member
Member
Joined
Oct 15, 2015
Messages
338
Trophies
0
XP
611
Country
Italy
I hope no

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)
 

randomdev

Well-Known Member
OP
Member
Joined
Jun 7, 2015
Messages
155
Trophies
0
XP
236
Country
Brazil
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)
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...)
 

Slashcash

Well-Known Member
Member
Joined
Oct 15, 2015
Messages
338
Trophies
0
XP
611
Country
Italy
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...)

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.
 
  • Like
Reactions: randomdev

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Veho @ Veho: Imagine putting your butt near it :tpi: