• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Random in C++ and Java

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am porting a program from C++ into Java. In the C++ program, there are a small function to make a good random number by declaring a loop with the limited value based on the time scaled back to range from 0 to 127 to run the rand () function. After that the final number will be make by using some formulas with the rand() function again. So, my question is, do i have do the same in Java?
 
Marshal
Posts: 79232
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends whether you are happy with the SecureRandom and Random classes.
 
Thai C. Tran
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Depends whether you are happy with the SecureRandom and Random classes.



Awesome ! I never known about the SecureRandom class before. Will have look at it now. Thank you very much
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what your application is about, but SecureRandom is meant for security-sensitive applications.

It's not really possible to generate truly random numbers with just software, because software works deterministically. There are many different algorithms to generate numbers that are a good approximation of true random numbers, but they're not really random. If you know what the algorithm does and what the seed value is, you can re-create the same sequence of random numbers. For security-sensitive applications, that's not good enough - if some cryptography algorithm depends on random numbers for security, and somebody can re-create the sequence of random numbers used in the security algorithm, then he or she can possibly break the security. So you have to take extra care with security-related use of random numbers, and that's what SecureRandom is for. For really sophisticated secure random numbers, you can get special hardware-based solutions which can generate true random numbers.

For other, more "normal" uses of random numbers (for example in games or statistical simulations) that's not an issue and you can just use java.util.Random.

You didn't say what your code is for, but if it is for some security-related application, you'll have to take this into account.
 
Marshal
Posts: 28220
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thai Tran Cao wrote:there are a small function to make a good random number by declaring a loop with the limited value based on the time scaled back to range from 0 to 127 to run the rand () function. After that the final number will be make by using some formulas with the rand() function again.



It sort of sounds like the writer of that code believed that if you combined several random values, that was somehow more random than just taking one random value. This isn't true (or if it is true, then the random values weren't really random in the first place).

Of course I may be applying a wrong interpretation of what you wrote there.
 
Thai C. Tran
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your kindly replies. I know what i should do now

@Jesper de Jong : It is really a security-related application
 
Campbell Ritchie
Marshal
Posts: 79232
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic