# randon number generator inside a function

• 04-29-2005
a1pro
randon number generator inside a function
hello

i've trying to generate a random number inside a function, but to no avail.

I've done the usual

Code:

void motion( int initial_position, float carrier_position[200],float x)// equation of motion in 1D. In nm
{
int counter;
double bulk_time=100e-12; //carrier decay time in Bulk AlGaAs
double velocity=2e13; // carrier speed in bulk GaAs

srand((unsigned)time(NULL));

x = (float) rand()/RAND_MAX;

for(counter=1;counter<=101;counter++)// 101 steps cover 2000 nm at 2e13 nm/s and 1ps time step
{
carrier_position[counter]=(float)initial_position+velocity*x*(time_step*counter-1e-12);
}
}

i've tried generating x inside the for loop, with srand outside as well, but to no avail.

I also tried generating the seed before calling the function in the main program

any ideas?

TIA
• 04-29-2005
major_small
put srand() where it'll only be called once per run of the program... i.e. in main().
• 04-29-2005
a1pro
that almost works
it works fine for all but the first run which hardly varies.
1st RN 1st run: 0.514481
2nd run:0.524552
3rd run: 0.526231

and so on, the others are fine, i.e. they change considerably from one run to the next.

Any ideas on how to solve this?

TIA
• 04-29-2005
joshdick
How to solve what? By dividing by RAND_MAX, you're always going to get a number less than or equal to one. If you want your random number to fall in a larger range, divide it by something less than RAND_MAX.
• 04-29-2005
a1pro
what i meant is that there is hardly any change in the first number generated while the second one varies from 0.002432 to 0.746566 in the first four runs.
• 04-29-2005
joshdick
They're (pseudo)random numbers. Any attempt to find order in them will likely be futile unless the pattern you deteced is somewhere along the lines of (A*x + B) % C.

You're likely to see little variation because you have such a small range. Also, if your seeding the RNG with the system time every time, then you're needn't worry about repetition.

If you're doing some heavy-duty stuff with random numbers, though, there are libraries out there that implement other generators. Nothing beats a good die, though :p
• 04-29-2005
a1pro
not entirely sure what is causing it, but there is something odd with the first number.

It doesn't matter too much, i'm just curious about it
• 04-29-2005
joshdick
Maybe because you're seeding the RNG with the system time and running consecutive trials of your program, you're first generated numbers tend to be similiar because the seed is.

But that's just a guess. They're (pseduo)random numbers, so even though you've witnessed this a dozen times over, it could change the thirteenth time you run it.
• 04-29-2005
major_small
if it's really bothering you that much, throw the first number away every time...

I would acutally suggest limiting your precision as well... for example, instead of getting a number between 0..1, get a number between 0..10,000 and divide by 10,000.

I don't know if/how that would help, but it's something to try.
• 04-29-2005
a1pro
it must be the system time thingy.

I might just throw the first number away like major small suggested

Thanks
• 04-29-2005
major_small
Quote:

Originally Posted by a1pro
it must be the system time thingy.

I kinda doubt that... once it's seeded, it's seeded... unless you try to reseed it...
• 04-29-2005
swoopy
I'm getting the same output. The first number generated is always close to 0.
Code:

0.0956755
0.373943
0.335063
0.104587
0.215735
0.987671
0.761681
0.453169
0.989715
0.0914945

Throwing away the first number sounds like a good idea.
• 04-29-2005
swoopy
Or you could use something with better time resolution to seed the random number generator. For example on windows, you can use GetTickCount() or some other system function.
Code:

#include <windows.h>
.
.
srand(GetTickCount());