-1

I'm a newbie, shifted from iOS to WP7.

I'm generating a random number series, which I want to store into an array. In iOS it was like this

for(int i=0; i < num; i++) {

        int rand = arc4random_uniform(70);

        if([rand_array containsObject:[NSNumber numberWithInt:rand]]) {

            i--;

        }

I've searched, googled but thought this is the place where I can ask a question. Kindly help me.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Xander
  • 902
  • 2
  • 14
  • 33
  • In order for this to be a constructive question, you have to explain what the loop does and ask a question about a specific problem. Maybe your code is just incomplete, but I can't figure out what it does. – Gabe Apr 28 '12 at 17:41
  • 4
    "I'm a newbie, shifted from iOS to WP7" - do yourself a favour, and learn C# *first*, ideally using console apps, so that you know the *language* before you end up with all the extra complications of UI. – Jon Skeet Apr 28 '12 at 17:41
  • Have you seen this : http://stackoverflow.com/questions/2351308/random-number-generator-in-c-sharp-unique-values – Paul Diston Apr 28 '12 at 17:42
  • @Gabe I've got the ans from Mr.Paul. Thanks sir, as I'm rushing heavily I needed this, – Xander Apr 28 '12 at 17:45
  • Thanks @JonSkeet, will consider your advice. – Xander Apr 28 '12 at 17:46

5 Answers5

2
int min = 1;
int max = 4;
int num = 3;
Random r = new Random();
Int[] ar ;
ar = new Int[num]; // Creates array with 3 palces {ar[0],ar[1],ar[2])
for(i = 0;i =< num - 1;i++) {
ar[i] = r.Next(min,max); // generate random number between 1-4 (include 1 & 4)
}

I think this is should work (or I didnt understand you). Good luck=]

yair144
  • 56
  • 2
  • 6
2

Enumerable.Range(1, 70) generates numbers from 1 to 70. Then we shuffle them like a deck of cards.

int[] randomNumbers = Enumerable.Range(1, 70).Shuffle(new Random()).ToArray();

This needs to be in a separate class in the same folder.

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random random)
{
    T[] list = source.ToArray();
    int count = list.Length;

    while (count > 1)
    {
        int index = random.Next(count--);
        yield return list[index];
        list[index] = list[count];
    }
    yield return list[0];
}

Is this what you wanted?

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
1

i don't see the sense in your code. I would use a List.

for(int i=0; i < num; i++) 
   {
    int rand = arc4random_uniform(70);//hope you know what you did here, I don't

    if(yourList.Contains(rand))
        i--;
    else
        yourList.Add(rand);
    }

if the list doesn't contain the random number, it will add it, otherwise it will just repeat.

roqstr
  • 554
  • 3
  • 8
  • 23
1

In C# do something like this:

List<int> numbers = new List<int>();

for ( int i = 0; i < num, i++ ) {

    int rand = GetARandomNumber();
    if ( !numbers.Contains( rand ) ) {
        numbers.Add( rand );
    } else {
        i--;
    }

}

You'd also probably do well to convert this to a while loop:

List<int> numbers = new List<int>();

while ( numbers.Count < num ) {

    int rand = GetARandomNumber();
    if ( !numbers.Contains( rand ) ) {
        numbers.Add( rand );
    }

}
robrich
  • 13,017
  • 7
  • 36
  • 63
1

It's simple, really! A direct port of your code would look something like:

List<int> rand_array = new List<int>();


for(int i = 0; i < num; i++)
{
    int rand = RandomHelper.GetInt(0, 70);
    if(rand_array.Contains(rand))
    {
        i--; 
        continue;
    }

    rand_array.Add(rand);
}

To generate random numbers in C# there is a class aptly called "Random". You only really need to use one instance of the Random class to generate numbers, so if you wanted something like this:

static class RandomHelper
{
    static Random rng = new Random(); // Seed it if you need the same sequence of random numbers

    public static int GetInt(int min, int max)
    {
        return rng.Next(min, max);
    }
}
Blam
  • 2,888
  • 3
  • 25
  • 39