I’m trying to get a list of 10 random numbers where no number repeats itself twice.
The errors I get are:
Assets/Scripts/Multiplex.cs(30,34): error CS0841: A local variable rand' cannot be used before it is declared Assets/Scripts/Multiplex.cs(30,29): error CS1502: The best overloaded method match for System.Collections.Generic.List.Add(int)’ has some invalid arguments
Assets/Scripts/Multiplex.cs(30,29): error CS1503: Argument #1' cannot convert object’ expression to type `int’
If I try to declare rand as an “int” I get another error that it “conflicts with a child block”…anyway, here’s the script… I will appreciate help!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//Get a list a 10 random numbers, but no number can repeat itself twice
public class Multiplex : MonoBehaviour {
private bool NewNumber;
private int counter;
List<int> randnumbers = new List<int>();
private int listrand;
void Start () {
NewNumber = false;
counter = 0;
while (NewNumber = false) { //this loop won't stop until it made sure that the number isn't found in the past list of numbers.
int rand = Random.Range(0, 10); //generates a random number from 0-10
foreach (int listrand in randnumbers) {
if (listrand != rand)
{
counter++; //checks how many time there's no match between the lotterized number and all the numbers in the past list
}
}
if (counter < randnumbers.Count)
{
NewNumber = true; //only if there is single mismatch should this be true
}
}
randnumbers.Add (rand); //the new number is added to the list
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//Get a list a 10 random numbers, but no number can repeat itself twice
public class Multiplex : MonoBehaviour {
private bool NewNumber;
private int counter;
List<int> randnumbers = new List<int>();
private int listrand;
void Start () {
NewNumber = false;
counter = 0;
int rand = Random.Range(0, 10);
while (NewNumber = false) { //this loop won't stop until it made sure that the number isn't found in the past list of numbers.
rand = Random.Range(0, 10); //generates a random number from 0-10
foreach (int listrand in randnumbers) {
if (listrand != rand)
{
counter++; //checks how many time there's no match between the lotterized number and all the numbers in the past list
}
}
if (counter < randnumbers.Count)
{
NewNumber = true; //only if there is single mismatch should this be true
}
}
randnumbers.Add (rand); //the new number is added to the list
}
}
Thank you. Now I have another question if you will… Why doesn’t Debug.Log print anything on the screen when I do Debug.Log (listrand) ?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//Get a list a 10 random numbers, but no number can repeat itself twice
public class Multiplex : MonoBehaviour {
private bool NewNumber;
private int counter;
List<int> randnumbers = new List<int>();
private int listrand;
void Start () {
for (int i=1; i==10; i++) {
NewNumber = false;
counter = 0;
int rand = Random.Range (1, 10); //generates a random number from 0-10
while (NewNumber = false) { //this loop won't stop until it made sure that the number isn't found in the past list of numbers.
rand = Random.Range (1, 10); //generates a random number from 0-10
foreach (int listrand in randnumbers) {
Debug.Log (listrand); //WHY DOESN'T IT PRINT ANYTHING?
if (listrand != rand) {
counter++; //checks how many time there's no match between the lotterized number and all the numbers in the past list
}
}
if (counter < randnumbers.Count) {
NewNumber = true; //only if there is single mismatch should this be true
}
}
randnumbers.Add (rand); //the new number is added to the list
//Debug.Log (counter);
}
/*if (randnumbers.Count == 9) {
foreach (int nums in randnumbers) {
Debug.Log (nums);
}
}*/
}
}
I meant on the console, yes. None of those filters are checked (if you’re refering to “Clear” “Collapse” “Clear on Play” “Error Pause”)…
does it give you error? because you already defined listrand. in debug.log set any variable name that is not defined like
Even if I remove its definition before the void Start it doesn’t appear to matter
I’m getting a warning that
Assets/Scripts/Multiplex.cs(9,22): warning CS0414: The private field `Multiplex.NewNumber’ is assigned but its value is never used
Well I eventually solved in by another method…literally used another method The “contain” one… makes much more sense than relooping.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//Get a list a 10 random numbers, but no number can repeat itself twice
public class Multiplex : MonoBehaviour {
private bool NewNumber;
private int counter;
List<int> randnumbers = new List<int>();
void Start () {
while (randnumbers.Count < 10) {
int rand = Random.Range (1, 11); //generates a random number from 1-10
if (randnumbers.Contains (rand) == false) {
randnumbers.Add (rand); //the new number is added to the list
}
}
foreach (int listrands in randnumbers) {
Debug.Log (listrands);
}
}
}