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,858
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
  • No one is chatting at the moment.
  • Sicklyboy @ Sicklyboy:
    @BigOnYa, I'll rip a bag off my Volcano to that, brother
    +2
  • a_username_that_is_cool @ a_username_that_is_cool:
    DDDS . Dual Developer Dedede System :)
    +1
  • BakerMan @ BakerMan:
    also happy birthday @Xdqwerty
    +1
  • BigOnYa @ BigOnYa:
    Is it me you're looking for?
  • MysticStarlight @ MysticStarlight:
    'Cause I wonder where you are
  • MysticStarlight @ MysticStarlight:
    wait I got the lyrics mixed up
    +1
  • BigOnYa @ BigOnYa:
    And I wonder what you do...Lol
  • BigOnYa @ BigOnYa:
    Its all good, I'm here to talk to you.
  • BigOnYa @ BigOnYa:
    Hello
  • BigOnYa @ BigOnYa:
    I've been playing fallout4 all day, I went into a elevator and it kept going, never stopped, waited 5 minutes, must of been a bug. How bout you, whatcha do today?
    +1
  • MysticStarlight @ MysticStarlight:
    I'm wondering why a stray tuxedo cat has a cloudy eye
  • MysticStarlight @ MysticStarlight:
    I've been playing TOTK all day also
    +1
  • MysticStarlight @ MysticStarlight:
    Oh you reminded me of those elevator nightmares
  • BigOnYa @ BigOnYa:
    Awesome game, fun how they force you to build, but you can build as you want
  • MysticStarlight @ MysticStarlight:
    It is super fun, I actually prefer it over BoTW
    +1
  • BigOnYa @ BigOnYa:
    The guy holding the sign always cracked me up, esp when you fail, and it falls
  • MysticStarlight @ MysticStarlight:
    Ah yes, that guy lol. I still need to find all his signs. The game has a lot to do though so I don't know what to finish first
  • MysticStarlight @ MysticStarlight:
    I'm 120 hours in and am still like 44.4% done
    +1
  • BigOnYa @ BigOnYa:
    Good times. Building is starting to get good around now for you. You making me want to make run thru again.
  • BigOnYa @ BigOnYa:
    Ok good chatting, enjoy your gaming, and avoid the lightning, or put your sword away. Good night.
  • K3Nv2 @ K3Nv2:
    All I gotta do is not spend $10 in the next 5 days and I'll break even this month lol
  • K3Nv2 @ K3Nv2:
    @BigOnYa, tell your wife not to forget her wallet
    K3Nv2 @ K3Nv2: @BigOnYa, tell your wife not to forget her wallet