Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have written a code to generate random number ranging 0.8 - 1.2
while executing the numbers above 1 coming correctly as 1.2,1.22,1.4 etc

but in the case of 0.xx its coming like .98,.89,etc
I don't need repeating values

tried with zeros but it's repeating the values for other variables also

i'm stuck with this

Need Help


Thank you

What I have tried:

var ans = 'NA';

if (Condition> 0)
        {
          Random random = new Random();
          double val = (random.NextDouble() * (1.2 - 0.8) + 0.8);
          ans= val.ToString("#.##");
        }


        var needed = ans;


this gives .98
Posted
Updated 20-Nov-22 19:29pm
v2
Comments
Graeme_Grant 20-Nov-22 23:03pm    
Your issue is unclear. Please show a specific example.

To add to what Greame says, don't do it like that.
Declare a single Random instance at class level:
C#
private Random random = new Random();
Or:
C#
private static Random random = new Random();
and us that instead. The reason why is simple: when you create a Random instance, it is initialized from the system clock which has a fint=ite resolution and modern processors are well and truly capable of executing many instructions during a single tick of the clock. As a result, if you create a Random instance each time you want to use it, there is a very good chance that you will get the same sequence repeatedly if you execute the same code in a loop.
Creating a single instance means that doesn't happen, unless the random sequence itself actually contains duplicates.
 
Share this answer
 
Comments
Richard Deeming 21-Nov-22 7:00am    
Be careful with the static instance; it's not thread-safe at all. Working with System.Random and threads safely in .NET Core and .NET Framework[^]
OriginalGriff 21-Nov-22 7:14am    
Yes - but given the skill level on show so far, I don't expect multithreading to appear any time soon! :D
Manojkumar-dll 22-Nov-22 0:15am    
Thank you sir it works fine the values are not getting repeated
Do you mean that you want a leading zero '0' for numbers less than 1?

Then you would need:
C#
string ans = val.ToString("0.##");

If you want the same number of decimals, then:
C#
string ans = val.ToString("0.##0");

or...
C#
string ans = val.ToString("N2");

Another way is:
C#
string ans = $"{val:N2}";
 
Share this answer
 
v4
Comments
Manojkumar-dll 20-Nov-22 23:17pm    
Thank you sir you're right
but there is a error showing as " A local or parameter named 'ans' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter C#"

i think this arises due to ans is declared as variable data type,if that's the problem means how can i solve that
Graeme_Grant 20-Nov-22 23:42pm    
I specified the type declaration for the example. How you use it is based on your own code.
Manojkumar-dll 20-Nov-22 23:45pm    
yes sir if i use the first example(0.##) means it doesn't exceed 1 but i need values upto 1.2
the sceond one (0.##0) gives like 0.768 but i need only 2 numbers after float
Graeme_Grant 21-Nov-22 0:02am    
They are only examples. How you use it is up to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900