May 2021 1 137 Report
random number c# wont generate same number 2s?

basicly im making a simple program that genrates numbers for a game 1 - 10 but it cant generate same number 2s its to decide which player operates which control

int sd;

Random rnd =new Random();

sd= rnd.Next(10);

is simple code for random number how ever i did then think to put all 55 numbers in a list box and use index to genrat random index number every time get that value then remove it from the index how ever this didnt work as well as id hope so id like to know if any 1 knows another way or if they can think of a betta way of doing it thro list box

Update:

sorry full code

int sd;

int b; b = 0;

for (int x = 0; x < this.listBox1.Items.Count; x++)

{

b++;

this.listBox1.SetSelected(x, true);

}

Random rnd =new Random();

sd= rnd.Next(b);

b = b - 1;

string s = this.listBox1.GetItemText(sd).ToString() ;

MessageBox.Show(s);

listBox1.Items.Remove(sd.ToString());

Update 3:

sorry full code

int sd;

int b; b = 0;

for (int x = 0; x < this.listBox1.Items.Count; x++)

{

b++;

this.listBox1.SetSelected(x, true);

}

Random rnd =new Random();

sd= rnd.Next(b);

string s = this.listBox1.GetItemText(sd).ToString() ;

MessageBox.Show(s);

listBox1.Items.Remove(sd.ToString());


Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments




Helpful Social

Copyright © 2024 Q2A.MX - All rights reserved.