6 Replies - 465 Views - Last Post: 06 December 2012 - 02:52 PM Rate Topic: -----

#1 frost-tien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 07-June 12

Random number Gen implementing

Posted 06 December 2012 - 01:20 PM

Ok sorry for the title i really didnt know how to title this question. So here it is i need to randomly generate a number between say 3 and 9, i found this code here that would do it.

  private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(3, 9);
        }



But i need the random number it generates to go into this line of code where the R is
int x = pbhorse1.Location.X;
      int y = pbhorse1.Location.Y;
      pbhorse1.Location = new Point(x + R , y);


ive thought i could do some thing like
int R = Random 
or
new Point(x + random , y);

but it says system.Random is a 'type' but is being used like a 'variable'
or am i going at this all wrong ,
and thank you for all your time and help

This post has been edited by frost-tien: 06 December 2012 - 01:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Random number Gen implementing

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: Random number Gen implementing

Posted 06 December 2012 - 01:35 PM

First your randomnumber method doesn't make use of the parameters its receiving.

private int RandomNumber(int min, int max)
      {
          Random random = new Random();
          return random.Next(min, max);
      }




Where you have 'R' substitute the method returning your random number.

pbhorse1.Location = new Point(x + RandomNumber(3,9) , y);


Its obvious you don't understand the use of methods or parameters.

I strongly urge you to stop working on developing your own program from scratch and dedicate some time to just learning the foundation principals of the language. Work a couple "Learn C# in 30 days" books from cover to cover. Just do the lessons and learn before trying to architect programs.


My standard beginner resources post - Updated DEC 2012

You can try to learn C# by dismantling snippets and googling terms - basically you can take a hit-n-miss, shotgun approach. Or you could just follow a proper training program whether it be a couple intro books, school, on-line course or series of tutorials like the ones we offer. I'll tell you from experience that just fumbling around in the dark and trying to teach yourself with no guidance doesn't work. Its like stumbling across a Harrier Jump Jet and trying to teach yourself how to fly with no background in piloting: You simply lack any groundwork to start from. How can you lay out your own training course if you don't already know the material? Would you go to a university where the teacher says "I don't know any of this but we'll fumble through it together?"

We have a tutorials section and a learning C# series of articles.

First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover. Do a couple hundred on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Oct 2012
Spoiler

Was This Post Helpful? 2
  • +
  • -

#3 frost-tien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 07-June 12

Re: Random number Gen implementing

Posted 06 December 2012 - 01:37 PM

Oh my god i cant believe it would be that simple thanks for the help i tried the RandomNumber but didnt put the (3,9) after words thanks
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: Random number Gen implementing

Posted 06 December 2012 - 01:39 PM

 int RandomNumber(int min, int max)

This has two parameters, right? This means if you want to replace R with this you need to feed it two parameters as well.

int R = Random 

'Random' isn't even the name of your method!

new Point(x + random , y);

Again.. same thing - random isn't the name of the function you highlighted.


You can use your method in place of variable name - as long as you feed it the right parameters.

pbhorse1.Location = new Point(x +  RandomNumber(<min value>, <max> value) , y);


The other option is you can just use the variable and cut out the need for a method.
Random random = new Random();

int x = pbhorse1.Location.X;
int y = pbhorse1.Location.Y;
pbhorse1.Location = new Point(x + random.Next(3, 9) , y);


screw it.. I should have refreshed first. Ha.. I was too busy winning things from my radio station.

This post has been edited by modi123_1: 06 December 2012 - 01:40 PM

Was This Post Helpful? 3
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,193
  • Joined: 02-June 10

Re: Random number Gen implementing

Posted 06 December 2012 - 01:43 PM

One other thing... YOu shouldn't keep making a new Random object in the method. That is going to reduce the actual randomness of the numbers selected. Make the object one time as a class-scoped object, then just get the .Next random from it as needed within your method.

Random random = new Random();

private int RandomNumber(int min, int max)
      {
          return random.Next(min, max);
      }

Was This Post Helpful? 1
  • +
  • -

#6 frost-tien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 07-June 12

Re: Random number Gen implementing

Posted 06 December 2012 - 01:45 PM

Both Advice were both helpful thanks you guys for your time and help
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Random number Gen implementing

Posted 06 December 2012 - 02:52 PM

You should note that the maximum is exclusive and not inclusive.
So you can easily do something like so.
var randitem = mycoll[rng.Next(0,mycoll.Length)];


This post has been edited by tlhIn`toq: 06 December 2012 - 02:55 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1