0

How to generate number from 1000 to 9999 with 1 increasing order ?

I tried with Random Class, it is working fine, but instead of generating random number, I want to generate number with in a range.

private int RandomNumber(int min, int max)
{
   Random random = new Random();
   return random.Next(min, max); 
}

Then I call this method :

int returnValue = RandomNumber(5, 20);
Noob_NoVoice
  • 217
  • 2
  • 10
  • try this out it may be helpful http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java – Big_t Nov 10 '12 at 06:25

4 Answers4

0

You can simply do it this way:

public double RandomNumber(int min, int max)
{
    return Random.NextInt(min, max) ;
}
Ahmad
  • 12,336
  • 6
  • 48
  • 88
0

How to generate number from 1000 to 9999 with 1 increasing order ?

You can use loop to generate number in range.

for(int i=1000; i < 9999; i++)
{
    Console.WriteLine(i);
}

You can use List to store the generated numbers.

List<int> lstNumbers = new List<int>();
for(int i=1000; i < 9999; i++)
{
   lstNumbers.Add(i)
}

You can use linq IEnumerable.Range() method as well.

IEnumerable<int> squares = Enumerable.Range(1000 , 9999).Select(x => x * x);
foreach (int num in squares)
{
      Console.WriteLine(num);
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • I am giving you one example. It might help you understanding the question. private int RandomNumber(int min, int max) { Random random = new Random(); return random.Next(100,200); } So it will give random number with range i.e : sometime 101, 123, 128 etc... But I want the number will generate like this : 101,102,103,104,105,106.....200. Hope it is clear to you. – Noob_NoVoice Nov 10 '12 at 08:26
  • The solution I gave already does what you are saying in example. You must forget Random as you do not want what Random offers so what is the point to use Random. Random is supposed to be out of sequence and you can not enforce Random class to generate number is sequence. You can use loop for this just start loop from your starting number instead of zero. – Adil Nov 10 '12 at 10:14
0

Calling Random.Next(int, int) will give you a random number in that range. You should clarify your question.

elucid8
  • 1,412
  • 4
  • 19
  • 40
0

the best way to do SQL Sequence and you call it every time, because the random number process will start over if you restarted IIS.

Saddam Abu Ghaida
  • 6,381
  • 2
  • 22
  • 29