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

• 07-16-2002
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;
--------------------------------------------------------------------
• 07-16-2002
I want to generate 1000 random numbers continue so I want to generate number not the same value at the same second.
• 07-16-2002
master5001
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.
• 07-16-2002
toaster
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.
• 07-16-2002
master5001
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).
• 07-16-2002
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;
--------------------------------------------------------------------
• 07-16-2002
toaster
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; }```
• 07-16-2002
toaster
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; }```
• 07-16-2002
Unregistered
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; }```
• 07-16-2002
raimo
Quote:

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.
• 07-16-2002
Salem
> 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
• 07-16-2002
toaster
Quote:

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).