randon number generator inside a function

This is a discussion on randon number generator inside a function within the C++ Programming forums, part of the General Programming Boards category; hello i've trying to generate a random number inside a function, but to no avail. I've done the usual Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    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
    Last edited by a1pro; 04-29-2005 at 08:28 AM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    put srand() where it'll only be called once per run of the program... i.e. in main().
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    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

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    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.
    Last edited by a1pro; 04-29-2005 at 09:05 AM.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    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

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    it must be the system time thingy.

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

    Thanks

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 09:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21