• Post Reply Bookmark Topic Watch Topic
  • New Topic

Math.random syntax  RSS feed

 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just started with Java.
I came across the Math.random syntax.
What are the different ways in which it can be used?

I used it in three different ways and got different answers.

Could you please tell me what are the different rules to use this syntax?

Here is how I used it

20170316_192543.jpg
[Thumbnail for 20170316_192543.jpg]
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns a random number between 0 to 1.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please post code as text rather than a screen image and be sure to UseCodeTags.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sakshi Jaine:

You can find documentation for Java classes either by Googling something like java 8 math random or looking it up from here:

https://docs.oracle.com/javase/8/docs/api/

But you should actually be using the newer Random class.

A note about your post: don't use screenshots or pictures for posting code.  Copy and paste the code to your reply and please UseCodeTags (that's a link) (see Bear's post for link).
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sakshi Jaine wrote:. . . Math.random . . .
What are the different ways in which it can be used? . . .
As you have been told it returns a double between 0.0 and 0.999999... I don't like doing arithmetic with it; as you will see here. You get so much more versatility with a Random object. I would use Math.random() only when I can cope with something under 1.0:-...will do something with a 25% probability and something different with 75% probability. I think that for every other random number, you shou‍ld use a Random object. Note the subclasses shown in that link, which can produce numbers for higher security.
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In math.random does the system choose all the possible selections before repeating any selection?

Example

String[ ] list1={1,2,3,4,5,6};
not one=list1.length;
not rand=(int)(Math.random ()*one);
String phrase=list1 [rand];
System.our.println (phrase);

What will happen if there are more than one array with there respective random user.
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we use random in such a way that it can make infinite selections?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sakshi Jaine wrote:In math.random does the system choose all the possible selections before repeating any selection?


No. That wouldn't be random. Consider tossing a coin: it's quite possible to throw two heads in a row. An algorithm which didn't allow that possibility wouldn't be random.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sakshi Jaine wrote:Can we use random in such a way that it can make infinite selections?


No. The selections are being made from a finite domain, so it's impossible to choose an infinite set from that.
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way in which we can selections from a domain so that there are no repetitions of the selection which have alrady been maid?
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(maid)made
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sakshi Jaine wrote:Is there any way in which we can selections from a domain so that there are no repetitions of the selection which have alrady been maid?
Yes, but that ceases to be random. It is called random selection from a declining population (or similar).
One way is to create a List of options and remove each chosen. Choose them not by choosing the value, but by choosing the index to remove.
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie
could you please explain it in a bit more detail.....☺

I have been learning java for a very little  time so I couldn't understand much.😕
 
Sakshi Jaine
Greenhorn
Posts: 26
Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
One way is to create a List of options and remove each chosen. Choose them not by choosing the value, but by choosing the index to remove.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of a deck of cards. You can shuffle the deck and randomly pick a card. If you don't want to pick that card again, then you will not put it back in the deck. The more cards you pick this way, the fewer cards will remain in the deck. If you do it long enough, you will run out of cards. Since you don't want repeats, the population of cards that you choose from becomes smaller as you discard more and more cards to choose from.

Think of Java's random generators as ones that will always put the cards back in the deck, therefore it is possible to get the same value multiple times.
 
Campbell Ritchie
Marshal
Posts: 56585
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . it is possible to get the same value multiple times.
Nice explanation

Randomness always permits duplicates. what you want is to remove something. Do you know what a List is? It is much easier to use than an array. You can do the same with an array:-
  • 1: Create an array with all the values in order.
  • 2: Randomly select one element.
  • 3: Create a new array one smaller than the original array.
  • 4: Copy all elements with indices smaller than your random number to the new array.
  • 5: Copy all elements with indices larger than the random number to the new array, but with an index one smaller.
  • But Lists make that much easier because they hide all those implementation details from you. As Junilu has said, you will eventually make your array so small it will have 0 elements. Then you stop selecting things.
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thankyou very much.
     
    Campbell Ritchie
    Marshal
    Posts: 56585
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a pleasure Do show us how far you have got.
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie
    I tried to do it the way you told and hot stuck up midway
    😐

    How do I copy the indices into the new array?
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    These are the ways I used math.random

    In the first two I got the same output
                                   - the first element of each array

    In the last one it gave random output.

    What is the reason for this?
     
    Campbell Ritchie
    Marshal
    Posts: 56585
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't know. You sh‍ould probably use a Random instance rather than Math#random. You ought tp provide more details about what you want and what you are getting so we can help you. We probably need to see some code, too.
     
    Sakshi Jaine
    Greenhorn
    Posts: 26
    Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is it


     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    The random() method returns a double value from 0.0 (inclusive) to 1.0 (exclusive), so this...

    Sakshi Jaine wrote:



    ... which takes the random number, and truncates the decimal portion -- and produces a value of zero. This is because the whole portion of the number is always zero.  In other words, the rand variable will always be set to zero, regardless of the random number that is generated.

    Henry
     
    Campbell Ritchie
    Marshal
    Posts: 56585
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have already given you a link: here it is again. Did you look at that link? I think you have fallen into the trap I described there, whereby arithmetic with Math#random can reliably return a result randomly selected between 0 and 0. I recommend you stop using Math#random and use a Random instance instead.
    You can get numbers between 0 inclusive and a larger number exclusive from a Random instance. That can match what you want for random selection from an array.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!