Thread: Ask about how to generate random number which is not the same value at the same sec.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Ask about how to generate random number which is not the same value at the same sec.

    I want to generate random number which not have the same value at the same second. If you know how to do that, please tell me.This below is some part of program which generate the same random number at the same second.



    code:
    --------------------------------------------------------------------
    srand((unsigned)time(NULL));
    random_number = (rand() << 15 + rand());
    random_number = random_number % MAX_RANDOM;
    --------------------------------------------------------------------

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Lightbulb

    I want to generate 1000 random numbers continue so I want to generate number not the same value at the same second.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you use the time like you are then technically you can't have this event happen for some time since that second isn't coming back for a while. However, you could make a table of random numbers:
    Code:
    int rtable[60];
    
    for(int i = 0; i < 60; i++)
       rtable[i] = rand_number_algorithm();
    This is not a specific example but you get the idea.

  4. #4
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    let me get this straight: you want to create 1000 random values so that no random values are identical, correct? but when you say at the same second, that throws me off. as far as I know, only a random value can be created at one time.

    if you want to create 1000 different random values, I suggest you use a loop and then compare the values for identicals. since the chances are greater with more random values, you can use an alternate which I call a "random shuffle technique".

    create an array (length is based on the range of the values) and fill it up in sequence.
    (example: for(x=MIN_RANDOM;x<=MAX_RANDOM;x++)... )
    then you just shuffle the array using the rand() function.
    using srand() is optional but preferred.

    I hope this helps, if at all.
    think only with code.
    write only with source.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    hmmm I was just reading toaster's post and realized that you didn't want any identical numbers. Do what I said but make your own table by hand. You don't want to use the rand() function since it produces a random number. You aren't really wanting a random number for what you doing it sounds like you just need some arbitrary number (not necessarily randomly generated).

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Thank you for your answer. I want to generate random number between 0-999999 so I write these code. Do you know how to edit my source to generate different random number. Can I use seeding?

    code:
    --------------------------------------------------------------------
    srand((unsigned)time(NULL));
    random_number = (rand() << 15 + rand());
    random_number = random_number % MAX_RANDOM;
    --------------------------------------------------------------------

  7. #7
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    anyway, here is just a sample source that will output 1000 random values using my alternate technique.

    Code:
    /* I assume you do realize that the difference between the minimum random value and the maximum random value is at least 1000 inclusively since you want a 1000 different random values.
     also note that the array will take up quite some memory.
     this is not the best algorithm but it will do. I did this quickly out of scratch without compiling it so errors may exist. */
    
    #include<iostream.h>
    #include<stdlib.h>
    #include<time.h>
    
    int main(void)
    {
     srand(unsigned(time(NULL))); using time as seed
     int array[1000];
     //for this example, I will use the random values from 0 to 999.
     for(int x=0; x<=999; x++)
     {
      array[x]=x;
     }
     //shuffle up the array
     int rand_value, temp_value;
     for(x=0; x<=999; x++)
     {
      // just a swap technique with rand()
      rand_value = rand()%1000;
      temp_value = array[x];
      array[x] = array[rand_value];
      array[rand_value] = temp_value;
     }
     // output values to screen
     // (change this if you don't want it to output to screen)
     for(x=0; x<=999; x++)
     {
      cout<<array[x]<<" ";
     }
     return 0;
    }
    think only with code.
    write only with source.

  8. #8
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    you know what, forget that idea of mine unless you are willing to drain memory.
    here is just a basic approach to your problem.
    like before, I did it out of stratch so there might be some errors laying around.

    Code:
    //note: this program may take a while to finish...
    #include<iostream.h>
    #include<stdlib.h>
    #include<time.h>
    
    int array[1000], x, y;
    
    bool check()
    {
     //check for identicals
     for(x=0;x<1000;x++)
     {
      for(y=0;y<x;y++)
      {
       if(array[y]==array[x]) { return true; }
      }
     }
     return false;
    }
    
    int main(void)
    {
     srand(unsigned(time(NULL)));
     bool loop=true;
     while(loop)
     {
      for(x=0;x<1000;x++)
      {
       array[x] = rand()%999999; //or you can use your code here
      }
      loop=check();
     }
     for(x=0;x<1000;x++)
     {
      cout<<array[x]<<" ";
     }
     return 0;
    }
    think only with code.
    write only with source.

  9. #9
    Unregistered
    Guest
    Here is a modified version of toaster's check-func.

    Code:
    bool check()
    {
     //check for identicals
     for(x=0;x<999;x++)
     {
      for(y=x+1;y<1000;y++)
      {
       if(array[x]==array[y]) { return true; }
      }
     }
     return false;
    }

  10. #10
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    you know what, forget that idea of mine unless you are willing to drain memory.
    Why do you think that the first solution "drains" memory? Your second code is more than O(n^3) and the first one is O(n). The memory consuming is the same. The second code you posted does useless work.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Do you know how to edit my source to generate different random number
    Yeah, call srand() just once, like the later example programs do

    Remember that for the small fraction of a second that your program runs for, time() is effectively a constant, so calling srand(time(NULL)); before every call to rand() will get you nowhere

  12. #12
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    Originally posted by raimo

    Why do you think that the first solution "drains" memory? Your second code is more than O(n^3) and the first one is O(n). The memory consuming is the same. The second code you posted does useless work.
    ooosawaddee3 is trying to generate 1000 random values from 0 to 999999. 1000 and 999999 is a big difference (despite the memory issues, it(my alternate algorithm)'s decently efficient if speed is more of a concern).

    I'm also pretty aware of my second version of the code (read the first comment in the code). I also did mention that the code is more of a basic approach since some programmers would use this type of technique when facing such a problem. I know I can optimize the code a little further but doing this at midnight just isn't the time for me (took me a few minutes to do that example).
    Last edited by toaster; 07-16-2002 at 11:46 AM.
    think only with code.
    write only with source.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generate Random Number
    By peacealida in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2008, 08:57 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  5. Replies: 2
    Last Post: 01-04-2004, 05:52 PM