• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

JAVA Random Generation

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I need a big help is there anyone who knows how to create a random generator in java. I am not talking about the built in one.

Thank You
 
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where exactly are you stuck?  
 
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try asking google for an algorithm to generate random number?
Once you have an algorithm, then work on the code to implement it.
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need one big help can anyone please give me their discord ID or something so that I can be on call and then solve those problem??
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:Where exactly are you stuck?  



basically I got one assignment and I am stuck, I can't do few things so it would be helpful if we can stay on a call and then solve the problem
 
Saloon Keeper
Posts: 6709
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not how these forums work. If you describe in detail where you're stuck, we'll help you get going, but nobody here is going to chat/phone you through it.
 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you done any research for an algorithm?   You will need that before you try writing any code.
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:That's not how these forums work. If you describe in detail where you're stuck, we'll help you get going, but nobody here is going to chat/phone you through it.


This is the question and I am still stuck at this I'm not getting how am I suppose to keep this whole array circular through my whole journey.

Suppose you have been hired to develop a musical chair game. In this game there will be
7 participants and all of them will be moving clockwise around a set of 7 chairs organized
in a circular manner while a music will be played in the background. You will control the
music using random numbers between 0-3.If the generated random number is 1, you will
stop the music and if the number of participants who are still in the game is n, the
participant at position (n/2) will be eliminated. Each time a participant is eliminated, a
chair will be removed and you have to print the player names who are still in the game.
The game will end when there will be only one participant left. At the end of the game,
display the name of the winner.
 
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rakibul Mahin wrote:...I am not talking about the built in one.


I think you'll have to be a bit more specific over the requirements. Also let us know why you are not allowed to use the built in Random generator that Java provides.
I believe that a truly random generator program may not exist. Over the years, a lot of pseudo random number generators have been created. Have a look at :
Linear_congruential_generator, Lehmer_random_number_generator. The simplest of all is the Middle-square method which does exactly what it says.

As an interesting discussion, there are implementations of random numbers from unexpected sources. e.g. sounds from a forest, the movement of liquids in a lava lamp and so on...
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took permission from our faculty and he said i can use built in random generator now i'm facing problem maintaing cirular array after each elimination.

 
Norm Radder
Rancher
Posts: 3968
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use an ArrayList to hold the participants? The indexing would go from the last element to the first element. Or a doubly linked list with the last element pointing to the first element.
 
Saloon Keeper
Posts: 4153
160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what is wrong with:

why would you need your own generator?

The outcome does not depend on the number of chairs or the length of the music playing. Are you perhaps modelling that famous game, often played at wedding parties?
 
salvin francis
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:... The outcome does not depend on the number of chairs or the length of the music playing. Are you perhaps modelling that famous game, often played at wedding parties?


I think OP's description is a variation of the game played at wedding parties. The game we play using using real people usually involves one less chair than the number of persons and when music stops, the running and pushing begins ;)

OP's variation might be as follows:

Let's assume that the people are numbered 1 to 7 as 1,2,3,4,5,6,7
assuming clockwise rotation, it would make the first iteration as 2,3,4,5,6,7,1
second iteration as 3,4,5,6,7,1,2
and so on...

There's a 1 in 4 chance of the music stopping at any iteration since we're looking at choosing a number at random between the intervals 0 to 3
Once the music stops, the person in the center leaves (I am assuming round off to highest value here), so if the music stops at second iteration, 7/2 ~= 4th person leaves.
New list would now have only 6 numbers: 3,4,5,7,1,2
and so on...


A few questions for OP:

Do let us know if my assumptions are correct. If yes, how are you going to solve this using code ?
Do you know how looping works ?
Do you know how to use Lists ?
 
Marshal
Posts: 71047
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weddings? Round here, it is usually played by children under the age of nine, usually on somebody's birthday.
 
Piet Souris
Saloon Keeper
Posts: 4153
160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Salvin
you don't need rotations. Start with a List of N persons, eliminate a Person at index random(N) (or swap it with element at N-1), then eliminate index random(N - 1), et cetera, until one Person remains..

But admittedly, using rotations and eliminating Person N/2 is much more interesting!  Also makes for a nicer GUI.
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so we are told that we need to use circular arrays and if our random number is not 1 then we have to keep rotating. And when we will get 1 then we will eliminate n/2 position player from the start, here n is number of players in game.
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

Piet Souris wrote:... The outcome does not depend on the number of chairs or the length of the music playing. Are you perhaps modelling that famous game, often played at wedding parties?


I think OP's description is a variation of the game played at wedding parties. The game we play using using real people usually involves one less chair than the number of persons and when music stops, the running and pushing begins ;)

OP's variation might be as follows:

Let's assume that the people are numbered 1 to 7 as 1,2,3,4,5,6,7
assuming clockwise rotation, it would make the first iteration as 2,3,4,5,6,7,1
second iteration as 3,4,5,6,7,1,2
and so on...

There's a 1 in 4 chance of the music stopping at any iteration since we're looking at choosing a number at random between the intervals 0 to 3
Once the music stops, the person in the center leaves (I am assuming round off to highest value here), so if the music stops at second iteration, 7/2 ~= 4th person leaves.
New list would now have only 6 numbers: 3,4,5,7,1,2
and so on...


A few questions for OP:

Do let us know if my assumptions are correct. If yes, how are you going to solve this using code ?
Do you know how looping works ?
Do you know how to use Lists ?



Yes, I also knew that this game is player with one less chair, but here it is different.

let me give you a small demo:

person = {"A","B","C","D","E","F","G"}

numberOfPersonAlive = 7

while numberOfPersonAlive != 1:
   generateRandomNumberBetween0to3 = generateRandom()
   if generateRandomNumberBetween0to3 == 1:
       remove player from (numberOfPersonAlive / 2) chair
       numberOfPersonAlive -= 1
   else:
       rotate


After one elimination I should have six person something similar to below:


person = {"A","B","C","D","E","F",null}









 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

Piet Souris wrote:... The outcome does not depend on the number of chairs or the length of the music playing. Are you perhaps modelling that famous game, often played at wedding parties?


I think OP's description is a variation of the game played at wedding parties. The game we play using using real people usually involves one less chair than the number of persons and when music stops, the running and pushing begins ;)

OP's variation might be as follows:

Let's assume that the people are numbered 1 to 7 as 1,2,3,4,5,6,7
assuming clockwise rotation, it would make the first iteration as 2,3,4,5,6,7,1
second iteration as 3,4,5,6,7,1,2
and so on...

There's a 1 in 4 chance of the music stopping at any iteration since we're looking at choosing a number at random between the intervals 0 to 3
Once the music stops, the person in the center leaves (I am assuming round off to highest value here), so if the music stops at second iteration, 7/2 ~= 4th person leaves.
New list would now have only 6 numbers: 3,4,5,7,1,2
and so on...


A few questions for OP:

Do let us know if my assumptions are correct. If yes, how are you going to solve this using code ?
Do you know how looping works ?
Do you know how to use Lists ?



yes you are correct
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Ok So I tried and I am stuck here I am not getting desired output
 
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rakibul Mahin wrote:Ok So I tried and I am stuck here I am not getting desired output



It's frustrating when that happens, but for programmers it's normal for code not to work right the first time. So what you need to do is this: you need to look at the output you get and examine how it differs from the desired output. That could possibly lead you to the code which is wrong.
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Rakibul Mahin wrote:Ok So I tried and I am stuck here I am not getting desired output



It's frustrating when that happens, but for programmers it's normal for code not to work right the first time. So what you need to do is this: you need to look at the output you get and examine how it differs from the desired output. That could possibly lead you to the code which is wrong.



I did and I can't find any error, basically I should have one winner and it should eliminate whenever random generated number i 1, but it is not happening help me.

 
Paul Clapham
Marshal
Posts: 26128
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, like I tried to suggest, you need a description of your problem. Here's a link to one of our FAQ entries: ItDoesntWorkIsUseless so click on it and read the suggestions there.
 
salvin francis
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rakibul Mahin wrote:...


That would make it a deadlier version of Russian roulette with a guarantee of 6 dead !!

Jokes aside.. I think that using an array is complicating your code. I understand that the requirements state that, but this code would be way simpler using another datatype that I hinted a while ago.
 
Rakibul Mahin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Finally I solved the Problem

Thanks for helping
 
salvin francis
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for sharing your solution with us. I'm glad to see it works as per your requirements. A few points from my end:

  • The "generate" method creates a new Random object every time whereas you just need to create it once. You don't even need this method actually since it's a one-line delegation method
  • All your methods are static which is why you are passing circArr to every method. Instead, consider making the array as a class variable
  • Since your loop termination event is well-known, don't use a while (true) loop. You can simply write it as while (newPerson.length > 1)
  • A lot of the method names don't show their intent

  • There's a few more suggestions, but I'll leave it here

     
    Master Rancher
    Posts: 3719
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [please ignore; I see my question was already answered]
     
    Would you like to try a free sample? Today we are featuring tiny ads:
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic