Jump to content

Random 3 digit float number (C++)

So recently I've been interested in coding in C++ and while I was interested in making a program involving purchasing an item I wondered how would I assign the float as a randomized 3 digit number while the decimal is between the 1st and 2nd digit to represent money, I would like it also to be capped at say the 9 Dollars range if so

 

#include <iostream>

 

using namepsace std;

 

int main()

{

         float Item = (the randomized number)

}

Link to comment
Share on other sites

Link to post
Share on other sites

rand(); creates a pseudo random integer.

(float)(ran() % 900); creates a pseudo random integer between 0 and 899 and converts to float

((float)(rand() % 900))/100; creates a pseudo random integer between 0 and 8.99

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, SCHISCHKA said:

rand(); creates a pseudo random integer.

(float)(ran() % 900); creates a pseudo random integer between 0 and 899 and converts to float

((float)(rand() % 900))/100; creates a pseudo random integer between 0 and 8.99

Thanks very much for explaining I was a tad lost haha

Link to comment
Share on other sites

Link to post
Share on other sites

@Daniel22

Take care if you use floating point for stuff like this. I float can't represent 0.01 (or even 0.1) exactly. So you have to take care with rounding or you loose a cent or two.

Mineral oil and 40 kg aluminium heat sinks are a perfect combination: 73 cores and a Titan X, Twenty Thousand Leagues Under the Oil

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Stefan1024 said:

Item = (float)(rand() % 1000); // 0 to 999

 

I assume you need:

Item = ((float)(rand() % 1000)) / 100; // 0 to 9.99

I noticed when using that method the price of what the item has been automated as was below 1 dollar I was just wondering how would I cap a minimum amount as well 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Daniel22 said:

I noticed when using that method the price of what the item has been automated as was below 1 dollar I was just wondering how would I cap a minimum amount as well 

Just add a minimum and adjust the range so your maximum doesnt increase as wel.

 

Item = ((float)(rand() % 9000)) / 100 + 1; // 1 to 9.99

Mineral oil and 40 kg aluminium heat sinks are a perfect combination: 73 cores and a Titan X, Twenty Thousand Leagues Under the Oil

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Stefan1024 said:

Just add a minimum and adjust the range so your maximum doesnt increase as wel.

 

Item = ((float)(rand() % 9000)) / 100 + 1; // 1 to 9.99

I actually just noticed with this method that it always turns out as .41 would you know why this happens?

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Stefan1024 said:

Just add a minimum and adjust the range so your maximum doesnt increase as wel.

 

Item = ((float)(rand() % 9000)) / 100 + 1; // 1 to 9.99

 

The current code I am testing this option out with

 

#include "stdafx.h"  // Visual Studio library
#include <iostream>

using namespace std;

int main()
{
    float Money;
    int End;
    
    Money = ((float)(rand() % 1000)) / 100 + 1; // 0 to 9.99

    cout << "Look how much money I have " << Item << " I have so much!";
    cin >> End;

    return 0;
}

Link to comment
Share on other sites

Link to post
Share on other sites

it's worth noting that rand() is pretty bad at what it does and is also a c function, not a c++ one (they are compatible of course, but it's not good practice to mix them). You should use the <random> library for any serious or semi-serious project.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Daniel22 said:

 

The current code I am testing this option out with

 

#include "stdafx.h"  // Visual Studio library
#include <iostream>

using namespace std;

int main()
{
    float Money;
    int End;
    
    Money = ((float)(rand() % 1000)) / 100 + 1; // 0 to 9.99

    cout << "Look how much money I have " << Item << " I have so much!";
    cin >> End;

    return 0;
}

It may create allways the same sequence of numbers and as you only use the first one it's the same. As @Sauron said better use the <random> library.

Mineral oil and 40 kg aluminium heat sinks are a perfect combination: 73 cores and a Titan X, Twenty Thousand Leagues Under the Oil

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Stefan1024 said:

It may create allways the same sequence of numbers and as you only use the first one it's the same. As @Sauron said better use the <random> library.

How might this option work?

Link to comment
Share on other sites

Link to post
Share on other sites

Would it be better to import a list of numbers I have made myself and one of them would be outputted to the program 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Daniel22 said:

Would it be better to import a list of numbers I have made myself and one of them would be outputted to the program 

Depends on the usage, but usually such look up tables must NEVER be used for any kinde of encription or security applications.

Mineral oil and 40 kg aluminium heat sinks are a perfect combination: 73 cores and a Titan X, Twenty Thousand Leagues Under the Oil

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, Daniel22 said:

How might this option work?

	//Include headers <random> and <cmath>

	//Do this once...
	std::uniform_real_distribution<double> dist(0.0, 9.0); //Uniform distribution on interval [0, 9)
	std::random_device rd; //Instantiate random device to get a random seed.
	std::mt19937 re(rd()); //Instantiate mt19937 random engine and seed with random device.

	//Do this every time you need a new random number.
	double item = round(dist(re) * 100) / 100;
	

As to why you should not use `rand()` anymore: https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/7/2017 at 5:46 PM, Stefan1024 said:

Creating true random numbers is extremly difficult for a computer. But you can get pretty good pseudo random once.

Considering the entire foundation of computation is deterministic behavior...but then again the fact that it is not truly random also has very important uses.

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, Yamoto42 said:

Considering the entire foundation of computation is deterministic behavior...but then again the fact that it is not truly random also has very important uses.

A lot of `std::random_device` implementations can provide pure entropy. It can be gathered from keyboard timings, mouse movements, noise captured from sound devices, etc... The fact that `random_device` can't be seeded illustrates that it's truly random, not a PRNG. Because of this it's slow tough. That's why, for applications that don't require cryptographic security, we use the random_device to provide the initial seed for the Mersenne twister PRNG, which is blazingly fast.

 

You are right tough, one of the main advantages of PRNG's is the reproducibility of a sequence for debugging/testing purposes.

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

×