Random method for a quiz

Each method contain a question with multiple choice. When i call the method in the main, i need to shuffle it and make sure there are no repetition.

public static void main(String[] args) { question_1(); question_2(); question_3(); question_4(); //continue to question 15 question_15(); }

thing that i tried.

int question_A = question_1(); int question_B = question_2(); int question_C = question_3(); int question_D = question_4(); //to question 15 int question_O = question_15(); //then i created an array int [] question = new int[14]; question[0] = question_A; question[1] = question_B; question[2] = question_C; question[3] = question_D; //to last array question[14] = question_O; //to random it here is the code Random r = new Random(); for (int counter = 0; counter <10; ++counter){ int swap_Index = r.next Int(15-counter)+counter; //there is an space between next Int, that because i was getting not properly formatted in the edit box int temp = question[counter]; question[counter] = question[swap_Index]; question[swap__Index] = temp; int[] question_To_Ask = new int[10]; for (int count = 0; count<10; ++count){ question_To_Ask[count] = question[count]; }

The reason the random does not work is because it starts executing the program at

int question_A = question_1();

for the random, i also tried any way such as Math.random. None of these worked and yeah, please do not use advance technique to solve this problem as i am a beginner.

-------------Problems Reply------------

The easy way to do this is using a list:

List<Question> questions = new ArrayList<Question>();
questions.add(question_1);
questions.add(question_2);
questions.add(question_3);
.....
Collections.shuffle(questions);

See, I dunno what you are doing with all these methods, but let us consider another approach put every question in a database, use a Collection like say hashmap in ur java code access ur database and based on the id of the question u can call whichever question u want to call and for the shuffling part there is a predefined function called shuffle in java, u can use it to shuffle ur question collection. Just a suggestion, try it, i think it is a better approach.

You could do something like this

public static void main(String[] args) {
// declare the variables first
int q1 = 1;
int q2 = 2;
...
int q15 = 15;
int[] questions = new int[] { q1, q2, q3, q4, ... q15 };

System.out.println("Before Shuffle");
for (int i : questions) {
System.out.println(i);
}

shuffle(questions); // here we do the shuffle

System.out.println("After Shuffle");
for (int i : questions) {
System.out.println(i);
}
}

public static void shuffle(int[] questions) {
Random random = new Random();

for (int i = 0; i < questions.length; i++) {
int newIndex = random.nextInt(questions.length - 1);
swap(questions, i, newIndex);
}
}

private static void swap(int[] questions, int oldIndex, int newIndex) {
int temp = questions[oldIndex];
questions[oldIndex] = questions[newIndex];
questions[newIndex] = temp;
}

Category:java Views:0 Time:2018-07-10

Related post

  • Write a truly inclusive random method for javascript 2010-01-27

    Javascript's MATH object has a random method that returns from the set [0,1) 0 inclusive, 1 exclusive. Is there a way to return a truly random method that includes 1. e.g. var rand = MATH.random()*2; if(rand > 1) { rand = MATH.floor(rand); } retur

  • Best method for storing quiz results in MySQL 2012-03-07

    I'm trying to record test/quiz scores in a database. What's the best method to do this when there might be a lot of tests and users? These are some options I considered: should I create a new column for each quiz and row for users, or does this have

  • Creating a random order for my quiz questions 2012-02-13

    I'm quite new to C# programming so bare with me. I've read through numerous questions already posted on the site but I can't seem to see one that poses the same problem and I believe there could be a more simple approach. I have created a quiz applic

  • create random integers for quiz using plist 2011-08-31

    I would like to have user click on a button to generate a ten-question quiz in the form of "a +/- b = c" where the values for a and b are from +10 to -10 and are randomly assigned for the ten questions. Also, the questions should randomly switch betw

  • Efficient method for randomly selecting all elements of a std::vector exactly once WITHOUT reshuffling 2011-12-19

    I am looking for an efficient method for selecting access to each element of a std::vector<T> in a random order, without reshuffling or copying them i.e no use of std::random_shuffle and ensure that each element is selected only once. I don't w

  • MySql database design for a quiz 2010-05-02

    I'm making an online quiz with php and mysql and need a bit of help deciding how to design the database for optimal insert of questions/answers and to select questions for the quiz. The table will hold 80 questions each with 4 possible options plus t

  • Non-random salt for password hashes 2009-02-11

    UPDATE: I recently learned from this question that in the entire discussion below, I (and I am sure others did too) was a bit confusing: What I keep calling a rainbow table, is in fact called a hash table. Rainbow tables are more complex creatures, a

  • generate random values for unit testing Hast table data type 2009-03-24

    My project generates few values(equal partitioning method) for each data types by getting the Minimum and Maximum values. I am doing this generating values for functional testing, I am actually passing this values to nunit partner, max amd min are ap

  • Passing a random method as a parameter? 2009-07-16

    Is there any way in C# to pass a random method as a parameter? To explain my question: I want to write a simple Logger-Tool that reports the entering and leaving of a method with the passed arguments an the class and method name: The log file I'm aim

  • How do I create a random method name 2010-03-12

    I plan on using JSONP to call an external webservice to get around the fact that I dont want to create a global function that could potentially conflict with the calling page. I thought that creating a random function name and passing it up would wor

  • Is there an encryption method for a column with a data type of int? 2010-03-23

    The scenario is that I want to encrypt finance numbers in a column with a data type of int in a sql server table. It is a big app so it is difficult to change the table column data type from int to any other data type. I'm using sql server 2005 and a

  • Of these 3 methods for reading linked lists from shared memory, why is the 3rd fastest? 2010-03-28

    I have a 'server' program that updates many linked lists in shared memory in response to external events. I want client programs to notice an update on any of the lists as quickly as possible (lowest latency). The server marks a linked list's node's

  • c++ generate a good random seed for psudo random number generators 2010-04-14

    I am trying to generate a good random seed for a psudo-random number generator. I thought I'd get the expert's opinions. let me know if this is a bad way of doing it or if there are much better ways. #include <iostream> #include <cstdlib>

  • Most reliable method for uploading files in PHP w/ progress bar 2010-05-27

    I am interested in finding the most reliable method for uploading files in PHP. I need a progress bar with the upload. I have tried SWFUpload but it randomly issues an I/O Error. Even if the same file is uploaded sometimes there is an error and somet

  • Best/standard method for slowing down Silverlight Prism module loading (for testing) 2010-08-29

    During localhost testing of modular Prism-based Silverlight applications, the XAP modules download too fast to get a feel for the final result. This makes it difficult to see where progress, splash-screens, or other visual states, needs to be shown.

  • General methods for optimizing program for speed 2010-11-16

    What are some generic methods for optimizing a program in Java, in terms of speed. I am using a DOM Parser to parse an XML file and then store certain words in an ArrayList, remove any duplicates then spell check those words by creating Google search

  • Random values for a range 2011-04-15

    I have the following code: Low = 10 High = 25 R = Int((High - Low + 1) * Rnd() + Low) Sheets("ITEMS DATA").Range("D4:D39").Value = R I would like for the range stated to return a random number, but it returns the same random number for the whole rang

  • Find a random method that best fit list of values 2011-04-25

    I have a list of many float numbers, representing the length of an operation made several times. For each type of operation, I have a different trend in numbers. I'm aware of many random generators presented in some python modules, like in numpy.rand

  • xcode do a random method 2011-05-04

    Hi every one I'm french so scuse me for my english. Well what I want to do is that every second I do a random method between four methods. how can I do this please ? --------------Solutions------------- Somewhere you need to start the timer [NSTimer

  • random font for website text? 2011-05-12

    I've seen lots of methods for generating random font colours, but how would I go about using a random font from a list of fonts (or just totally random) for a section of text on my website? --------------Solutions------------- You can get a random en

  • Best way to generate random images for moving top to down 2011-05-27

    I am creating a game which have similar concept like Tap Tap Ant. I am generating dynamic images using NSTimer using following code. NSTimer *tmrGenerateImages1 = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(generateImag

  • Best PHP hashing method for storing user passwords in a MySQL table? 2011-06-24

    I've been reading Stack Overflow questions for about 15 minutes now and every single one seems to contradict the previous one I read. Bcrypt, SHA1, MD5, and so on. I currently MD5 my passwords, but I want to make my database more secure in case of a

  • Trying to create a method that chooses a random method from that same class 2011-07-16

    Maybe this is a stupid idea... I am new to Ruby (and to OOP, so I still dont really know what I am doing most of the time), and I thought of a small, fun project to build, and I am struggling with some concepts. What I am building is basically a stri

  • How to make the compiler choose the non-template version of a method for derived classes? 2011-08-15

    I'm writing a matrix class (CMatrix), with such derived classes as a 3d vector(CVector) and a rotation matrix(CRotMatrix). My CMatrix object could be multiplied by an another CMatrix-based object or by an any numerical value(scalar). This code repres

  • Best method for passing Data from Java/JSF2 bean to Javascript/jQuery Components 2011-10-28

    I am having trouble nailing down the best method for passing data from a Java backing/managed bean to a jQuery/Javascript component such as Highcharts, so that my web app produces/displays data in a dynamic, real time way. I am pretty solid on my Jav

Copyright (C) dskims.com, All Rights Reserved.

processed in 1.348 (s). 12 q(s)