Help - Search - Members - Calendar
Full Version: Is It Just Me Or Is C# Random Terrible?
BleepingComputer.com > Software > Programming
   
Ryan 3000
I'm using C#'s random function right now to generate different locations for cloud sprites so that I can make a unique cloud each time. I gave the random a range of 100. What always happens is all 40 sprites appear within 10 pixels of each other. Can you explain why the random is not working? Java's seemed to work much better!

BTW THIS IS MY CODE:
CODE
               x = new List<float>();
            y = new List<float>();
            for (int i = 0; i < 40; i++)
            {
                Random random = new Random();
                x.Add((float)random.NextDouble() * 100);
                y.Add((float)random.NextDouble() * 100);
            }
Ryan 3000
Sorry guys, problem solved. Turns out that making a random in the for loop each time resulted in the following, or i'm guessing:
it came up with the same random number because it started over each time, which means it could not tell itself that it had given me the same number every time. I put the random initializer out of the for loop and everything works great now.

Here's a cloud I made with it:

The green is the ground BTW.
groovicus
I've never done that before......

whistling.gif

(Easy mistake to make)
Ryan 3000
I have another question and this one has been haunting me ever since I got into C#. When I make structs, which are like classes within classes, the struct cannot use variables of its container class. This means that in a game with a struct called bullet, the bullet cannot access the main class for information. What this means is I have to make a big long list of parameters for bullets with every variable of the class to put its information in the struct. It makes me so angry, I want to access these variables, how can I do it? (Yes, both class, struct, and all variables thereof have been made public.)
groovicus
A structure is a value type (or a data structure), like an int or an array. Since they are data structures, they have no concept of scope, any more than in int or array have a concept of scope. Classes are reference types; they refer to data types, and thus know about scope.

Clear as mud?
Ryan 3000
Is there a way around this? What I've done before is I've made an item receive itself in its constructor, for example a class called Main has Main inside, a variable it can pass on as a parameter to other things. Can I say 'this' instead of using a variable like that?
groovicus
I don't think so. Just use an inner class; When it comes down to it, a class can be a data structure also. I will agree it is a bit confusing because the lines sort of get blurred between reference types and data types.
Ryan 3000
I'm hitting a big problem here. The C# compiler keeps compiling an older version of code. For example, I will write more code to add a method and when I try to use it, the compiler says it doesn't exist. When I try to add to a public struct's public variable, it says this variable does not exist. What's going on? I've saved it and reopened it, debugged and built it several times...

the exact error is this:
CODE
what i'm trying to do is:
cloud[i].x += 0.5f;

"Error    1    Cannot modify the return value of 'System.Collections.Generic.List<WindowsGame1.Game1.Cloud>.this[int]' because it is not a variable    J:\C Sharp\Cloud Generator\WindowsGame1\Game1.cs    149    21    WindowsGame1
"
groovicus
I would need to see the rest of your code.
Ryan 3000
CODE
public struct Cloud
        {
            public float x, y;
         }
public void Update()
{
        cloud[i].x += 0.5f;
}
"Error    1    Cannot modify the return value of 'System.Collections.Generic.List<WindowsGame1.Game1.Cloud>.this[int]' because it is not a variable

or if you need even more:
CODE
        public struct Cloud
        {
            public float x, y;  //represents approximate center of cloud, all cloud's particles are moved around it
            public List<double> xList, yList; //contains the location of every particle in the cloud. is fed a random list in the constructor
            public Cloud(float x1, float y1, List<double> xList1, List<double> yList1)
            {
                x = x1; //center X of cloud
                y = y1; // center y of cloud
                xList = xList1; //location X of individual particles
                yList = yList1; //location Y of individual particles
            }
        }
(update)
            for (int cloudIndex = 0; cloudIndex < clouds.Count; cloudIndex++)  //cloudIndex represents each cloud 'clump', a group of cloud particles
            {
                for (int particleIndex = 0; particleIndex < clouds[cloudIndex].xList.Count; particleIndex++)  // particleIndex manages each individual texture of each cloud
                {
                    clouds[cloudIndex].x += (0.5f);  //add 0.5 to the X pixel location of each cloud
                    clouds[cloudIndex].y += (0.5f); // same for Y
                    if ((clouds[cloudIndex].yList[particleIndex] + clouds[cloudIndex].y) > 750) //see next line
                        clouds[cloudIndex] = new Cloud(-200, random.Next(-200,500), randomList(50), randomList(50));  //generates a new cloud if current one goes off screen
                    if ((clouds[cloudIndex].xList[particleIndex] + clouds[cloudIndex].x) > 950) // same as above
                        clouds[cloudIndex] = new Cloud(random.Next(-200, 700), -200 , randomList(50), randomList(50)); //same
                }
            }


!!!! meh, problem solved I suppose. Instead of modifying the x and y of the center of each cloud, I modified the location of each particle. It works okay i guess but I would still prefer to move entire cloud at once.
thanks for much help groovicus
if you want to see the code in action video here:
clouds are good
Alan-LB
Thats cool - I like it!!!!

Alan
Wolfy87
Ok i need to do somethink like that...well i need to random and im a beginer and Groovicus has referd me some code buttt...i dont have a clue how to use it. here is the code he gave me...... \/ \/ \/ \/

int minimumValue=0;
int maximumValue=5;
Random rNum = new Random();
int randomNumber= rNum.Next(minimumValue,maximumValue);


I dont have a clue how to implement it...can you help?...im using C#

Thanks, Ollie.
Ryan 3000
Ollie, you can use that code to generate a number between minimumValue and maximumValue, or in the instance of your code, a number between 0 and 5. Just put randomNumber where you would normally use an int but where you want the number to be random.

For example, I could make a random between 1 and 4 to decide which of the four tires on my car would pop.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.