Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tried to generate random numbers n i was using winform c# code . i want to send generated random number at a time to database and my mail.here the problem is every time previous generated random num is getting concatenated .but this should not happen how do i restrict it.

What I have tried:

public static string strrandom = string.Empty;

      void Generate_OTP()
      {

         }
Posted
Updated 6-Jun-17 21:30pm
v2
Comments
Richard Deeming 7-Jun-17 11:53am    
Removing the content of your question after it has been answered is EXTREMELY rude.

1 solution

Simplest solution is to return the value, instead of using a "global variable":
private static Random objran = new Random();
private static string Generate_OTP()
    {
    string strRandom = "";
    string chars = "0123456789";
    int No_of_Characters = 6;
    for (int i = 0; i < No_of_Characters; i++)
        {
        int pos = objran.Next(1, chars.Length);
        string newChar = chars[pos].ToString();
        strRandom += newChar;
        chars = chars.Replace(newChar, "");
        }
    return strRandom;
    }
You then just use it like this:
SQL.Parameters.AddWithValue("TEXT_ME_RESET_PASSWORD", Generate_OTP());
 
Share this answer
 

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