Jump to content

Even numbers on randomly generated floats (Unity - C#)

minibois

Very basic and probably easy question, but I cant find an answer only and I can't think of anything now.

How can I make it so the random generated floats are even number? Or even in 0.5f increments?

 

The code that generates the numbers:

EnemyPositions = new Vector2(Random.Range(20.0f, 28.0f),Random.Range(-4.4f, 4.4f));

So it makes a random float between 20.0f and 28.0f and also another random float between -4.4f and 4.4f.

This is an example of floats that I get returned from this:

gallery_73344_3516_4144.png

These are two sets of random generated floats and they are quite near each other in value

 

Thanks :D

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

I don't quite get it.

 

What is an even float number?

Something like 2.0? Is 2.1 even? Or is it something like 3.4 ?

 

Do you want to get random numbers varying with 0.5 ?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't quite get it.

 

What is an even float number?

Something like 2,0? Is 2,1 even? Or is it something like 3.4 ?

 

Do you want to get random numbers varying with 0.5 ?

Should have explained, float is like a double or number variable. So a number which can have decimals (as opposed to Int for example, which can't have decimals) and it has an f behind it, since that variable is used in another sort of circumstance. Anyways

I want to make my random numbers without decimals (so the random number between 20.0f and 28.0f can only be 20.0f, 21.0f, 22.0f, etc.) 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Should have explained, float is like a double or number variable. So a number which can have decimals (as opposed to Int for example, which can't have decimals) and it has an f behind it, since that variable is used in another sort of circumstance. Anyways

I want to make my random numbers without decimals (so the random number between 20.0f and 28.0f can only be 20.0f, 21.0f, 22.0f, etc.) 

I know what floating point numbers are, I didn't get what you meant.

I understand now though.

You could generate a random int in your range and multiply it by 1.0f.

Or just convert that int to a float.

And if you want to do in increments of 0.5 , create a random int in double your range (for your example , [20.0, 28.0] would become [40.0,56.0]) then multiply it by 0.5f.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

If you are not worried about rounding then just cast to an int. (this causes truncation e.g. numbers after the decimal point are discarded.) 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×