Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. random NUmbers

random NUmbers

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestionlounge
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ib042129
    wrote on last edited by
    #1

    Ok, here's the deal. I got an "array" of 60 normal variables and a separate "array" of 10 filler (fake) variables I need to go through the "array" of 60 and as I do I need to randomly place all the variables of the "array" of 10 between some of those of the first array if I just do something like if(rand()%(60/10)) use normal; else use filler; I might not get all ten variable in. What algorythm should I use to randomly run each of the 10 variables between the 60 normal ones, and to make sure I do not get a systematic large chunk of normal variables? P.S. I don't know if what I said is very clear, sorry about that....

    J 1 Reply Last reply
    0
    • I ib042129

      Ok, here's the deal. I got an "array" of 60 normal variables and a separate "array" of 10 filler (fake) variables I need to go through the "array" of 60 and as I do I need to randomly place all the variables of the "array" of 10 between some of those of the first array if I just do something like if(rand()%(60/10)) use normal; else use filler; I might not get all ten variable in. What algorythm should I use to randomly run each of the 10 variables between the 60 normal ones, and to make sure I do not get a systematic large chunk of normal variables? P.S. I don't know if what I said is very clear, sorry about that....

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Something like this should work:

      int fillers_used=0;
      for(int i=n;i<60;++i){
      if(fillers_used<10 && rand()%((60-i)/(10-fillers_used))) {
      //use normal
      }
      else{
      // use filler
      ++fillers_used;
      }
      }

      Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      I 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        Something like this should work:

        int fillers_used=0;
        for(int i=n;i<60;++i){
        if(fillers_used<10 && rand()%((60-i)/(10-fillers_used))) {
        //use normal
        }
        else{
        // use filler
        ++fillers_used;
        }
        }

        Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        I Offline
        I Offline
        ib042129
        wrote on last edited by
        #3

        Thanks a lot... but this way wouldn't the frequency of the filler used be increased as I go through the array? (nnnnnnnnfnnnnnnnfnnnnnfnnnnfnnfnnff)?

        J 1 Reply Last reply
        0
        • I ib042129

          Thanks a lot... but this way wouldn't the frequency of the filler used be increased as I go through the array? (nnnnnnnnfnnnnnnnfnnnnnfnnnnfnnfnnff)?

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          Well there's a deficiency due to truncation problems, I guess you should use something like this instead:

          static double rand01()
          {
          int r;
          while((r=rand())==RAND_MAX);
          return (double)rand/RAND_MAX;
          }
          ...
          if(rand01()<(double)(10-fillers_used)/(60-i)){
          ...

          Apart from this, the process is unbiased. To prove this, we can use a little probability. Let's call Pn the probability that the process uses a filler value at position n. Also, let p=10/60, q=1-(10/60).Then: _P_0 = 10/60 _P_1 = _P_0·(9/59)+(1-_P_0)·(10/59) = (10/60)·(9/59)+(50/60)·(10/59) = 590/3540 = 10/60 P__n = p__n·((10-n)/(60-n)) + p__n-1·q·((10-(n-1))/(60-n)) + ··· + q__n·(10/(60-n)) It can be formally proven that P__n = 10/60 for every n, but also a small test program doing the calculations will convince you. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups