Zolomon

Hello!

I wish to create 5 unique random numbers, between 1 and 30. All the five numbers should be different from each other, example: 5, 21, 16, 28, 4 and not like: 2, 23, 14, 23, 30!

I have tried to accomplish it with this code:

Code Snippet

private void CreateRandomNumbers()

{
win1 = CheckNewNumber(win1);
lblWin1.Text = win1.ToString();
win2 = CheckNewNumber(win2);
lblWin2.Text = win2.ToString();
win3 = CheckNewNumber(win3);
lblWin3.Text = win3.ToString();
win4 = CheckNewNumber(win4);
lblWin4.Text = win4.ToString();
win5 = CheckNewNumber(win5);
lblWin5.Text = win5.ToString();
}

// This method creates a random number.
private void NewRandomNumber()
{
Random rand = new Random();
ARandomNumber = rand.Next(1, 30 + 1);
}

// This method checks if the new number exists, and
// creates a new one in case.
private int CheckNewNumber(int Test)
{
int random = 0;

NewRandomNumber();
random = ARandomNumber;

return random;
}

But for some reason I get like: 17, 23, 23, 23, 23

What should I do

Cheers,
Zolomon

### Re: Visual C# Express Edition Generate 5 unique random numbers?

IsshouFuuraibou

Do not initialize a new random number generator each time.

The other thing you should do is use a distribution/mapping formula to truely emulate the random. Since you are basically picking numbers out, you need to adjust the random probability for all the other numbers: first pick is 1/30 for any number, second pick is 1/29, third is 1/28, fourth is 1/27, fifth is 1/26. Statistically this is a very large difference even if it doesn't seem so.

Code Snippet

Random rand = new Random();
List<int> RemainNums;

private void CreateRandomNumbers()
{
RemainNums = new List<int>();
RemainNums.AddRange( new int[] { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30 } );
... // the regular code here
}

// This method creates a random number.
private void NewRandomNumber()
{
int index = rand.Next( 0, RemainNums.Count - 1 );
ARandomNumber = RemainNums[index];
RemainNums.RemoveAt( index );
}

Here is an example of how you could do a mapping function, you're just using a list, your random is actual an index into that list. After saving the random number, you remove that number from the list thus making it impossible to get the same number twice in a generation. It also adjusts your probabilities for you.

### Re: Visual C# Express Edition Generate 5 unique random numbers?

Zolomon

Aha..!

Thank you, IsshouFuuraibou! Seems like I've forgotten some mathematics during the summer vacation. This was incredibly helpful and exactly what I needed, most kind!

Regards,
Zolomon

### Re: Visual C# Express Edition Generate 5 unique random numbers?

how do i use this code to use a button to activate it and the result into a text box

### Re: Visual C# Express Edition Generate 5 unique random numbers?

how do i use this code so that i activate by pressing a button in info path and the code apearing in a text box in another part of the form

also on vsta/ visual basic where do i put the code

your help would be greatly appreciated

ps is this the full code

Random rand = new Random();
List<int> RemainNums;

private void CreateRandomNumbers()
{
RemainNums = new List<int>();
RemainNums.AddRange( new int[] { 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30 } );
... // the regular code here
}

// This method creates a random number.
private void NewRandomNumber()
{
int index = rand.Next( 0, RemainNums.Count - 1 );
ARandomNumber = RemainNums[index];
RemainNums.RemoveAt( index );

### Re: Visual C# Express Edition Generate 5 unique random numbers?

IsshouFuuraibou

no, the code is a snippet building upon the understood code, and was designed to be a replacement of the problematic code only. There is another thread somewhere (here) I have provided a sample code/project showing how to do lottery style random number generation. Granted that was addressing the issue of animating the results, but it will still be a good demo.

To access it from a button, you would add this to your class then inside the button click event:

private void button_Click( object sender, EventArgs e )
{
//Create a list of new numbers
CreateRandomNumbers();
// Or create a single random numbers
NewRandomNumber();
}

Unfortunately this code is very spicific to fixing the original problem. Since the original problem is C# you won't be able to copy the code into a VB project. If you're having a problem and need help, I would suggest starting a new thread and ask regarding your specific problem. We'd be happy to help with your specific problem.

### Re: Visual C# Express Edition Generate 5 unique random numbers?

hi

i was also trying to do this but also kept failing

i found a website that gave you the basic code and you can change the code in note pad/dreamweaver

URL: sorry cant find it

anyway here is the code ps ive tinkered around with it so if you want a 30 long password to be generated replace

<option value="15">N/A</option>

with

<option value="30">N/A</option> N/A is the display -I put this in because my needs were that users could not tamper with

the settings as the program i created to use the code to load the info path form to send the tickets to the remote kiosk ready to be printed relide on the code the user keyed in and he code accosiated with the tickets to be the same 15 long code

im sure youll work out the rest just dont change the base code!!!

also the pass code box is read-only however you can copy from it if you would like this disabled change

<input name="passbox" type="text" tabindex="1" value="(BOOKING PASSCODE)" size="24" readonly="true">
with

<input name="passbox" type="text" tabindex="1" value="(BOOKING PASSCODE)" size="24" readonly="false">

ps the reason i wanted the program and the code gen is for my project at school i need to create a theme park website

and making a online booking service would boost me up 3 grades or so for html an java usage!!

heres the code for you

just delete the WHY DO I NEED THIS link

<html>
<title></title>
<style>
a:visited{color:black;font-family:verdana}
a:hover{color:blue;font-family:verdana}

td {color:black;font-family:verdana;font size:8pt}
p {color:black;font-family:verdana;font size:8pt;text-decoration: none}
h1 {color:black;font-family:verdana;font size:12pt;text-decoration: none}
</style>
<body>
<script>
{
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
pass = "";

for(x=0;x<length;x++)
{
i = Math.floor(Math.random() * 62);
pass += chars.charAt(i);
}

return pass;
}

function formSubmit()
{
return false;
}

</script>
<h1 align="center"></h1>
<form name="passform">
<p align="center">
<select name="length">

<option value="15">N/A</option>

</select></p>
<p align="center">Passcode:
<input name="passbox" type="text" tabindex="1" value="(BOOKING PASSCODE)" size="24" readonly="true">
</p>
<p align="center">
<input name="button" type="button" tabindex="2" onClick="javascript:formSubmit()" value="Generate">
&nbsp;
<input type="reset" value="Clear" tabindex="3">
</p>
<p align="center">
Why do I need this
</A>

</p>
</form></td></tr></table>

</body>
</html>