Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
How to generate random number in c# using random function, new/next number is grater than previous one...?
Posted

You start with MSDN and read the documentation.
Random Class[^]
You have some pretty good samples there.

To check if the new number is greater than the previous, you need to store the old value and then compare with the new.
This can be done in a loop until your condition is fulfilled.
 
Share this answer
 
If you have to generate a finite sequence of increasing random numbers then you might follow this approach: generate the numbers using the standard random generator and store them into an array. Finally sort the array and use its items as the required (increasing) sequence.
 
Share this answer
 
Hi,

You can generate an initial random number and then sum that number to the previous generated one.

C#
Random r = new Random();
List<double> numbers;
numbers.Add(r.next());
for(int i = 0;i<whatever_limit;i++){
numbers.Add(r.Next + numbers[i]);
}


Hope this helped.

LG
 
Share this answer
 
v2
Comments
Zoltán Zörgő 5-Oct-14 16:32pm    
My 5.
This is the simplest approach that fulfills the requirement. But even better, is is of polinomial complexity over all other suggestions, that are all exponential.
LLLLGGGG 6-Oct-14 6:57am    
Thanks! :-)
Here's a function that will generate a list in ascending order of randomly chosen integers in a certain range; every value in the list will be unique(no duplicates):
C#
// requires Linq !
using Linq;

public List<int> generateRandomList(int sizeOfList, int min, int max)
{
    // add #1 to max value, so results include max value
    max++;

    // create, seed the random generator
    Random RandomGenerator = new Random(Guid.NewGuid().GetHashCode());
    
    List<int> randomIntResults = new List<int>();
    
    int currentInt = RandomGenerator.Next(min, max);
    
    for(int x = 0; x < sizeOfList; x++)
    {
        // keep going until you have a value that's not already in the List
        while (randomIntResults.Contains(currentInt))
        {
            currentInt = RandomGenerator.Next(min, max);
        }
        
        randomIntResults.Add(currentInt);
    }
    
    // use Linq to order the results in ascending order
    return randomIntResults.OrderBy(item => item).ToList();
}
If I wanted a List of 100 integers in the range 1~1000 chosen randomly:
C#
List<int> randomInt100 = generateRandomList(100, 1, 1000);
Note there's no argument checking done above: you could do something stupid that would create an endless loop, like:
C#
List<int> randomInt100 = generateRandomList(100, 1, 10);
 
Share this answer
 
v4
try this :

private string GetRandomString()
        {
            Random rand = new Random((int)DateTime.Now.Ticks);
            int randomIndex1 = rand.Next(100, 999);
            int randomIndex2 = rand.Next(10000, 99999);
            string randstring = RandomString(3);
 
            string result = "#" + randstring + "-" + randomIndex1.ToString() + "-" + randomIndex2.ToString();
            return result;
        }
 
        private static Random random = new Random((int)DateTime.Now.Ticks);
        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
 
            return builder.ToString();
        }


CSS
U can also refer this link

http://stackoverflow.com/questions/1122483/c-sharp-random-string-generator[^]
 
Share this answer
 
Comments
CHill60 6-Oct-14 2:29am    
Well it has the word "random" in there, but apart from that has little in common with the OP's question
Leena 206 6-Oct-14 2:40am    
in that case, you have to store previous generated random number in one variable and compare it with newly generated, if its grater accept that else regenerate..
CHill60 6-Oct-14 18:20pm    
Converting it to a number first obviously :-p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900