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

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

This is a discussion on Ask about how to generate random number which is not the same value at the same sec. within the C++ Programming forums, part of the General Programming Boards category; I want to generate random number which not have the same value at the same second. If you know how ...

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.

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.

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

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;
}

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.

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.

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