Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Is It Just Me Or Is C# Random Terrible?


  • Please log in to reply
13 replies to this topic

#1 Ryan 3000

Ryan 3000

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:02:44 PM

Posted 30 May 2008 - 09:34 PM

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:
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);
			}

Edited by Ryan 3000, 30 May 2008 - 09:39 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

BC AdBot (Login to Remove)

 


m

#2 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:02:44 PM

Posted 30 May 2008 - 10:32 PM

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:
Posted Image
The green is the ground BTW.

Edited by Ryan 3000, 31 May 2008 - 02:32 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:44 PM

Posted 31 May 2008 - 12:51 PM

I've never done that before......

:thumbsup:

(Easy mistake to make)

#4 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:02:44 PM

Posted 31 May 2008 - 01:09 PM

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.)
No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:44 PM

Posted 31 May 2008 - 04:01 PM

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?

#6 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:02:44 PM

Posted 31 May 2008 - 04:36 PM

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?
No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:44 PM

Posted 31 May 2008 - 05:06 PM

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.

#8 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:02:44 PM

Posted 31 May 2008 - 05:43 PM

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:
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
"

Edited by Ryan 3000, 31 May 2008 - 05:45 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:44 PM

Posted 01 June 2008 - 10:53 AM

I would need to see the rest of your code.

#10 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:02:44 PM

Posted 02 June 2008 - 12:27 PM

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:
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

Edited by Ryan 3000, 02 June 2008 - 01:29 PM.

No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#11 Alan-LB

Alan-LB

  • Members
  • 71 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Junee, NSW, Australia
  • Local time:05:44 AM

Posted 03 June 2008 - 12:35 AM

Thats cool - I like it!!!!

Alan
There are 10 types of people - those who understand binary and those who don't!!

Today is the Beta version of Tomorrow!

#12 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:06:44 PM

Posted 16 August 2008 - 01:33 PM

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.

#13 Ryan 3000

Ryan 3000
  • Topic Starter

  • Members
  • 834 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Maryland
  • Local time:02:44 PM

Posted 17 August 2008 - 11:28 PM

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.
No pessimist ever discovered the secrets of the stars, or sailed to an uncharted land.

#14 Hans_Hackfresse

Hans_Hackfresse

  • Members
  • 2 posts
  • OFFLINE
  •  
  • Local time:01:44 PM

Posted 27 November 2008 - 08:52 PM

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:

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
"



Hey, I've got the same problem yesterday. lalala is not a variable! My first thought was: your mother is not a variable, damn compiler. But after a while I figured it out a little bit.

I got the problem with silverlights Point class. It's not possible to just set the X or Y property. Here is an example:

class Point{double x, y;  public X  {    get { return x; }    set { x = value; }  }  public Point (double x, double y)  {     this.x = x;     this.y = y;  }}Point p = new Point ();p.X = 10; // This should get you the same error// But what you can to, to work around the error is the following:Point p2 = new Point (20, 50);// now increase x by 10:p2 = new Point (p2.X + 10, p2.Y);

That's the only way I found to solve the problem. But it works pretty well

Damnit. I just registered to give you this information, and now I see, it's a little bit late ... at least a few months :D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users