- Home /
Random Number Generator
Hello my question is how to code a random number generator. what im trying to do is make it so that when my heroe gets within one square of an enemy (which will be represented as a "?" until this happens) that the tile will randomly pick a number between 1 and 254 and a monster will be accompanied with that number and when the number is picked the tile will change into the monster. how could i do this, thanks in advance!
Answer by Chesley · Aug 26, 2012 at 08:13 PM
var number = Random.Range(1,254);
thanks! now when the number is picked how would i code it to change instance into the enemy that number is accompanied with
oh okay sry what im trying to say is that each enemy in the game has a number. and each enemy in the game in a "?" until its revealed. what i want to to is that when the enemy is revealed the tile with the "?" will randomly generate a number and the number thats picked then want the tile to turn into the enemy thats number is for
Answer by Chesley · Aug 26, 2012 at 08:26 PM
to detect wether the player is within one square of the question mark you can either use OnTriggerEnter (if you have a trigger on the game object) or you can calculate the distance between the objects and then if(distance < 1) and trow the random generator in that.
then do what ever you want with that number
okay i can do the trigger effect and changing the form and stuff. but whats the code for the number that was picked, for example how would i code: the number that was randomly generated to appear on the page
heres an example with GUI
var number : int;
function Start() {
number = Random.Range(1,255);
}
function OnGUI() {
GUI.Label(Rect(10,10,100,30), "The number that was generated: " + number);
}
edited btw sorry forgot one thing
oh okay thanks if i wanted for example if i wanted to show a random pic out of three first how would i link each number to each pic and then how would i code so that the number that was picked would so the number that was linked to it how would i go about doing this
okay maybe this might be better, is there anything like a random monster spawner. like could i make it so that when the monster is revealed theres a 50% chance it might be one monster and 50% it might be another, is there anything like that
Answer by CausticLasagne · Dec 14, 2015 at 12:12 PM
If you want truly random numbers, This is what I use. I generated 250 different numbers.
Random.seed = (Random.Range(Random.Range(Random.Range(Random.Range(0, 25), Random.Range(324, 5673)), Random.Range(Random.Range(53, 2378), Random.Range(50, 423))), Random.Range(Random.Range(Random.Range(23, 2354), Random.Range(1, 3456)), Random.Range(Random.Range(7, 32421), Random.Range(8, 23472)))));
Then use
int myInt;
myInt = Random.Range(0,25);
Just a tiny bit in excess, but it will give you a new number each time. Try not to call it too many times though :D - CausticLasagne
This is pointless and doesn't result in "truly random numbers". You can skip all that and just do Random.Range(0, 25).
This will not result in a random number. But would be a waste of resources. Ins$$anonymous$$d set the seed to the (int)System.DateTime.Now.Ticks should give a "random" seed everytime.
That's also a waste of resources. The point of setting a seed is to get a repeatable sequence. If you just want random numbers, call Random.Range without doing anything else. Don't bother setting the seed to the time; it's already doing that by itself.
Answer by ginryu · Nov 17, 2021 at 06:30 PM
int[] seedArray; List seedList = new List(); for(int i = 0;i<1000;i++){ int newSeed = Random.Range(0,64)+Random.Range(0,64)+Random.Range(0,64)+ Random.Range(0,64)+Random.Range(0,64)+Random.Range(0,64)+Random.Range(0,64); seedList.Add(newSeed); } seedArray = seedList.ToArray);
do not forget to use System.Linq; to convert from a generic list to an array. by using this as a source, it should be possible to create a list of seeds that can be used to create a more randomized feel from a Psuedorandom number generator