Solved

Random number

Posted on 2013-06-24
16
402 Views
Last Modified: 2013-08-03
Hi All,

I want to create random number for transaction.
The random number is not just from 1 - 10, when we have 10 transaction then we have 1 to 10.

I mean the number created not a serial number.
And the random number created using keys provided by us.
Some kind of systematic readable encryption.

How could I do it ?

Thank you.
0
Comment
Question by:emi_sastra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 39273964
The following code will generate you a random number based on two inputs

Public Class Form1
    Dim result As String
    Dim rand As New Random()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        rand.Next(
    End Sub

    Private Sub GenerateRandom(numberOne As Integer, numberTwo As Integer)
        result = rand.Next(numberOne, numberTwo)
    End Sub
End Class

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39273983
Hi  angus_young_acdc,

Your code seems not solve my question.
Your code will create a serial number at the end.

numberOne = 1
numberTwo  = 10
At the end we will have a serial number from 1 to 10.

The random number is not just from 1 - 10, when we have 10 transaction then we have 1 to 10.

I mean the number created not a serial number.

Thank you.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39274536
Struggling to understand. If you create random numbers between 1 and 10, you will not get serial numbers. You may get 3, 1, 6, 9, ... etc. If you wish, you can create random numbers between 1 and 100 or 1 and 1000.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:emi_sastra
ID: 39276720
Hi CodeCruiser,

What I want is to create a number, not just a random number, what I mean random number is a number that is not easy to read, thus not easy to duplicate, for example, voucher.

In the past I've heard about a number that has checked digit. If the number after reversed has not the same checked digit, then the number is invalid number.

Thank you.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39278214
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39278513
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39279495
Looks good.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39295393
I am sorry, just back.

I try to convert it to VB, but failed.

 private string GetUniqueKey()
  {
  int maxSize  = 8 ;
  int minSize = 5 ;
  char[] chars = new char[62];
  string a;
  a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  chars = a.ToCharArray();
  int size  = maxSize ;
  byte[] data = new byte[1];
  RNGCryptoServiceProvider  crypto = new RNGCryptoServiceProvider();
  crypto.GetNonZeroBytes(data) ;
  size =  maxSize ;
  data = new byte[size];
  crypto.GetNonZeroBytes(data);
  StringBuilder result = new StringBuilder(size) ;
  foreach(byte b in data )
  { result.Append(chars[__b % (chars.Length - )>); }
   <span class="code-keyword">return result.ToString();
  }

What should I do ?

Thank you.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39297353
Where did you get that code? Following does not look right

 <span class="code-keyword">return result.ToString();
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39298760
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39300093
That code contains html tags for some reason. Here is the converted code

private Function GetUniqueKey() As String
  Dim maxSize As Integer  = 8 
  Dim minSize As Integer= 5;
  Dim chars As new char(62)
  Dim a As string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
  chars = a.ToCharArray()
  Dim size As Integer  = maxSize 
  Dim data As new byte(1)
  Dim crypto As new RNGCryptoServiceProvider()
  crypto.GetNonZeroBytes(data) 
  size =  maxSize 
  data = new byte(size)
  crypto.GetNonZeroBytes(data);
  Dim result As new StringBuilder(size) 
  foreach b As byte in data 
     result.Append(chars(b % (chars.Length - 1))
  Next
  return result.ToString()
  End Function

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39301139
Hi CodeCruiser,

I've compiled error :

Error      8      Character is not valid.       
Error      7      Expression is of type 'Byte', which is not a collection type.       
Error      3      Type 'Byte' has no constructors.       
Error      5      Type 'Byte' has no constructors.       
Error      1      Type 'Char' has no constructors.
Error      4      Type 'RNGCryptoServiceProvider' is not defined.       
Error      6      Type 'StringBuilder' is not defined.       
Error      2      Value of type '1-dimensional array of Char' cannot be converted to 'Char'.       

Thank you.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39301627
Another try

Private Function RNGCharacterMask() As String
	Dim maxSize As Integer = 8
	Dim minSize As Integer = 5
	Dim chars As Char() = New Char(61) {}
	Dim a As String
	a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	chars = a.ToCharArray()
	Dim size As Integer = maxSize
	Dim data As Byte() = New Byte(0) {}
	Dim crypto As New RNGCryptoServiceProvider()
	crypto.GetNonZeroBytes(data)
	size = maxSize
	data = New Byte(size - 1) {}
	crypto.GetNonZeroBytes(data)
	Dim result As New StringBuilder(size)
	For Each b As Byte In data
		result.Append(chars(b Mod (chars.Length - 1)))
	Next
	Return result.ToString()
End Function

Open in new window

0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39301718
What is  RNGCryptoServiceProvider ?

Error      1      Type 'RNGCryptoServiceProvider' is not defined.

Thank you.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39301831
0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 39301975
Great.

Thank you very much for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 54
vb.net winforms sizing/resolution? 4 54
MailAddress in vb 4 36
Crystal reports login after closing form 3 34
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question