Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Robomaster · Aug 26, 2012 at 08:04 PM · randomnumbergenerator

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Chesley · Aug 26, 2012 at 08:13 PM

var number = Random.Range(1,254);

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Aug 26, 2012 at 08:17 PM 1
Share

You mean (1, 255).

avatar image Chesley · Aug 26, 2012 at 08:18 PM 0
Share

whoops, i forgot that for a moment

avatar image Robomaster · Aug 26, 2012 at 08:20 PM 0
Share

thanks! now when the number is picked how would i code it to change instance into the enemy that number is accompanied with

avatar image Chesley · Aug 26, 2012 at 08:21 PM 2
Share

i do not understand what you just said

avatar image Robomaster · Aug 26, 2012 at 08:26 PM 0
Share

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

Show more comments
avatar image
2

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robomaster · Aug 26, 2012 at 08:39 PM 0
Share

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

avatar image Chesley · Aug 26, 2012 at 08:44 PM 0
Share

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

avatar image Robomaster · Aug 26, 2012 at 09:04 PM 0
Share

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

avatar image Robomaster · Aug 26, 2012 at 09:20 PM 0
Share

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

avatar image
-1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Dec 14, 2015 at 07:39 PM 4
Share

This is pointless and doesn't result in "truly random numbers". You can skip all that and just do Random.Range(0, 25).

avatar image pet1100 · Sep 10, 2018 at 01:12 PM 0
Share

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.

avatar image Eric5h5 pet1100 · Sep 10, 2018 at 05:26 PM 1
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RNG output to a variable + instantiating a prefab based on said variable? 1 Answer

Random texture changer (problem with array) 2 Answers

Randomly generated number 0 Answers

Random.Range(..) not working 1 Answer

ArgumentException: RandomRangeInt can only be called from the main thread. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges