GP32 Random Function


nadir

Still Fresh
Joined
Jul 5, 2006
Messages
4
hi
I am written a program that use rand function
The program compile and run on my PC with out any problem.
but; The program compile and run on GP2X, But rand will not work, i do not get any out, if i remove rand from my program and set a fixed number i get an output.

i have made test program for rand, the program compile but rand is not working

this the Code:
srand( (unsigned)(time( NULL )+ dice_value*SDL_GetTicks()) );
rand_num=rand();

Do any one know why or have other function to use that work with GP2X (arm-gcc)

Not: i am new in programming
 
nadir posted on Jul 11 2006 at 06:21 AM said:
rand_num=rand();
That should be
rand_num=rand()%num;
where num is an integer number of how many you wish to random between so that you'll get a number between 0 and num.
 
Last edited by a moderator:
Shouldn't matter, it litterally locks up the GP2X. deadlychicken22, what header files are you using?
 
Code:
 #include <stdio.h>

#include <stdlib.h>

#include <iostream>

#include <fstream>

#include <unistd.h>

#include <string>

#include <cmath>

#include <SDL/SDL.h>

#include <SDL/SDL_image.h>

#include <SDL/SDL_ttf.h>

#include <SDL/SDL_mixer.h>
Hope this helps
 
i've used rand as well and it worked fine - i've not got the way i called it or the includes to hand but i will post them up tomorrow morning

[EDIT]
i seeded it as follows :

srand(time(0));//seed random

then used it like this :

possCHOSEN =(int)rand()%(possCHOICE);

Apologies - just realised this is GP32 and not 2X related so my input may not be relevant
[/EDIT]
 
I've used this for random terrain generation, I hope it's of use:
Code:
typedef signed int s32;
typedef unsigned int u32;
typedef signed short s16;
typedef unsigned short u16;

static s32 Seed=0;

s32 Rand(  ){
  static s32 Last;
  u32 T;
  u16 u, l, p;
  if( Seed ){ Last=Seed; Seed=0; }
  T = *(u32 *)&Last;
  Last=0;
  for( p=0; p<32; p++ ){
	u = (u16)((T&0xFFFF0000)>>16);
	l = (u16)((T&0x0000FFFF));
	T = p+((u32)(u-l)*(l+u));
	Last |= (T&0x1)<<p++;
	u = (u16)((T&0xFFFF0000)>>16);
	l = (u16)((T&0x0000FFFF));
	T = p+((u32)(u-l)*(l+u));
	Last |= (T&0x1)<<p;
  }
  return Last;
}

s32 sRand( s32 s ){
  Seed = s;
  return Rand();
}
 
Thank you all, i will try these different solution as soon as i get back home on Saturday
 
int rand_num, dice_MAX=6;

srand( (unsigned)(time( NULL )+ dice_value*SDL_GetTicks()) );
rand_num=rand()%dice_MAX;


At last it is working after 2 week. Thank you all a lot :D :D :D :D :D :D :D :D :D :D
 
Back
Top