Random Numbers

Hello every one...i need little help....i want to know that is there any way to use rand to generate random numbers between two limits let say from 0 to 100 such that one number comes only one time....if it is possible then how can i do that...thanks
Last edited on
I can point out at least two ways to achieve this. The first is to use std::set for keeping random numbers. The second is to use std::bit, for example, std::bit<100> and to set a corresponding bit for a given random number.
Last edited on
int Rand(int a, int b)
{
// return a random number in range [a, b]
time_t t;
t=time(NULL);
int r=t%rand();
r=(r*r+rand())%1010101;
r=2*r+rand();
return a+r%(b-a);
}

a, b is a range to limit.
@ledien
Why do you think that you guarantee unique random numbers?
o_O what is that, ledien?

A simpler way to produce numbers in range [a,b]:

1
2
3
4
5
int Rand(int a,int b)
{
  int range = b-a+1;
  return (rand() % range) + a;
}


But even that is not really what the OP is asking for. He wants to produce a sequence of non-repeating numbers.

I recall answering a question very similar to this recently:

http://cplusplus.com/forum/general/71261/
thank u all for your help....thanks very much :-).......
Topic archived. No new replies allowed.