Random, non-overlapping output from 31 arrays of elements [closed]

Asked 2 years ago, Updated 2 years ago, 16 views

Do you want to improve this question?Edit your post to clarify the issue you are trying to resolve by adding details.

Closed 4 years ago.

4 years ago

I'm writing a useful program for my mother, who is a teacher, but no matter how much I try, I don't know.

It is a program that randomly selects 31 students in the school class.I'd like to be nominated once per person and have everyone speak on the 31st, but I can't express it because I can name the same person several times and I want to make a program that prints numbers (attendance numbers) randomly without duplication.

None of the programs I wrote are complicated, but just to explain,

function choice: variable r, array liRandom, 31 if statements depending on the value taken by variable r

Variable r—A number +1 that can be taken randomly within the length range of the array liRandom is substituted.

Array liRandom—Contains iZero to iThirty; array liRandom is 0 to 30, total 31, variable r is 1 to 31, total 31.When r is 1, liRandom is 0 and when r is 31, liRandom is undefined, but I think this is the best way to suppress errors.

idzOne~zThirtyOne—Indicates the student's seat on the table, and each seat swings zOne, zTwo and id.To check the seat chosen by the variable r

ida1 to a31: A result selected by the variable r is prepared to be kept as a history.Rewrite to variable r in document.getElement

Array littleOut: If you add the value selected by the variable r to the beginning, the second and third of the array will be the previously allocated number.There is no second or third one from the beginning, so it is undefined, but if statement rewrites undefined to blank

id subText, threeText—Output the second and third of the array littleOut to this id so that you can see the numbers you pressed once and twice before

It's like this.Please use the stance that leaves the original model as much as possible.Please reply.(Thinking of kneeling down 100 times)

javascript

2022-09-29 21:13

1 Answers

I'm impressed that you're writing a useful program for my mother.

Then don't do anything difficult and use Excel or Google Sheets.
Use the RAND function to sort randomly and that's it.If you google it, you'll find out how to do it.

You can also use JavaScript-based AppScript for Google Sheets.If you program it, you can write it in a few lines.

function myFunction(){
  vars = SpreadsheetApp.getActiveSpreadsheet();
  varsheet=ss.getSheets()[0];
  for(vari=1;i<32;i++){
    sheet.getRange(i,1).setValue(i);
    sheet.getRange(i,2).setValue(Math.random());
  }
  sheet.sort(2, true);
};

If you print the array randomly and without duplication, JavaScript can write in a few lines in a similar way.If you want to leave a prototype, add your own program.


2022-09-29 21:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.