Author Topic: Problem with random numbers  (Read 2428 times)

ciuffolo

  • Guest
Problem with random numbers
« on: December 01, 2012, 12:47:52 am »
i think this isn't a sdk problem, but when i use the rand()%a+b function (return a random number between b and a) of c++,everytime i launch the program it create the same random number, for example:
first time i type the function and start the programm, it generate 4
second time i start the programm, it generate 4
and so and so on
i think that on 480 possibility isn't possible that generate 4 everytime!
how to resolve this problem?
I find Randomize() but i realized that this function don't resolve this problem.

graham

  • Guest
Re: Problem with random numbers
« Reply #1 on: December 01, 2012, 11:42:34 am »
Here's a solution for you:

Code: [Select]
#include "DragonFireSDK.h"

int i;

RandomSetSeed(TimerGetMs()); // Initialise the random number generator with a (practically) random seed
i = Random(100);             // This and all subsequent random numbers will be different each time you run the program
« Last Edit: December 01, 2012, 11:44:25 am by graham »

ciuffolo

  • Guest
Re: Problem with random numbers
« Reply #2 on: December 02, 2012, 03:43:44 pm »
thank you,i'm going to try it.

Posly

  • Guest
Re: Problem with random numbers
« Reply #3 on: December 06, 2012, 03:18:26 pm »
This video might also help:

http://www.youtube.com/watch?v=naXUIEAIt4U

Good luck

ciuffolo

  • Guest
Re: Problem with random numbers
« Reply #4 on: December 26, 2012, 03:14:55 am »
I have a little problem.
I have to use it for code that calculate a random position of an object that spawns every second.
you can use something else instead of TimerGetMilliseconds?

graham

  • Guest
Re: Problem with random numbers
« Reply #5 on: December 26, 2012, 03:58:00 am »
you only need to set the seed once, before you start using Random()


ciuffolo

  • Guest
Re: Problem with random numbers
« Reply #6 on: December 27, 2012, 03:25:27 am »
I thought may remain as usual if i don't change
thankyou