random number generator in c#

Updated on August 10, 2017 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on August 8, 2017

hey , I’m just brushing up on my c# and i could really use some help as to what the function is to call in the random number generator in c# on Xcode and how to get it to please help and thank you

  • Liked by
Reply
6 on August 8, 2017

How about this:

 

 
 
//static class and method
public static class Digitz
 {
      public static int GetRandomNumber(int min, int max)
     {
            Random r = new Random();
            int value = r.Next(min, max);
            return value;
      }
 }
 
///to be called like this 
int myRandomInteger = Digitz.GetRandomNumber(0, 1000);    
 
//will return random number between 0 and 1000
 

 

I am not sure what Xcode is, but if it has .NET references then this will work. 

on August 8, 2017

i’ll give it a try , thanks and Xcode is apple’s version of microsoft virtual studio 

on August 8, 2017

#include <iostream>

int main(int argc, const char * argv[]) {

    srand(123);

    int randon_number = rand();

    int a =rand();

    int b =rand();

    printf(“the random numbers generated are %d and %d\n”,a,b);

    

    

    return 0;

}

 

 

 

 

thats the code i tried typing to just get two random numbers generated and printed but sadly i always just get two of the same random numbers generated .

on August 8, 2017

and the code you sent didn’t work @s1l3nt

Wise
on August 8, 2017

“microsoft virtual studio” i now officially want to kill myself.

Devoted
on August 9, 2017

@noob That’s C++…

on August 10, 2017

oops my bad i meant to say microsoft’s visual studio… my bad ,  alright thanks for the correction @leon 

Show more replies
  • Liked by
Reply
Cancel
0 on August 8, 2017

You must declare that class outside of your main class. Then the call to the method can be made.

However, a more simple way to get a random number (with.NET) is just these lines:

//random value type
Random r = new Random();
//assign the "next" random to an integer
int myRandom = r.Next(0, 1000);    
Console.WriteLine(myRandom.ToString());    //print the int as a string

 

If that doesn’t work, Xcode does not reference .NET… and in that case, I am not sure.

  • Liked by
Reply
Cancel