bibekyess commited on
Commit
a8c2362
·
1 Parent(s): 8626d26

Maybe random generator fixed

Browse files
Files changed (1) hide show
  1. script.js +16 -12
script.js CHANGED
@@ -7,24 +7,27 @@ async function fetchRestaurants() {
7
  return data;
8
  }
9
 
10
- function getRandomRestaurants(restaurants, year, month, day) {
11
- // Generate a seed based on the current date and time
12
- const seed = year * 10000 + month * 100 + day;
 
13
 
14
- // Shuffle using seeded random
15
- for (let i = restaurants.length - 1; i > 0; i--) {
16
- const j = Math.floor(seededRandom(seed + i) * (i + 1));
17
- [restaurants[i], restaurants[j]] = [restaurants[j], restaurants[i]];
 
 
18
  }
19
- return restaurants.slice(0, 3);
 
 
 
20
  }
21
 
22
  async function displayRestaurants() {
23
  const today = new Date();
24
  const day = today.getDay();
25
- const year = today.getFullYear();
26
- const month = today.getMonth() + 1; // Months are zero-based
27
- const date = today.getDate();
28
 
29
  // Format the date
30
  const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
@@ -55,7 +58,8 @@ async function displayRestaurants() {
55
  try {
56
  const restaurants = await fetchRestaurants();
57
  console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
58
- const randomRestaurants = getRandomRestaurants(restaurants, year, month, date);
 
59
  console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants
60
  document.getElementById('restaurants').innerHTML = randomRestaurants.map(restaurant =>
61
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`
 
7
  return data;
8
  }
9
 
10
+ function seededRandom(seed) {
11
+ var x = Math.sin(seed++) * 10000;
12
+ return x - Math.floor(x);
13
+ }
14
 
15
+ function getRandomRestaurants(restaurants, seed) {
16
+ // Generate three random indices based on the seed
17
+ const indices = [];
18
+ for (let i = 0; i < 3; i++) {
19
+ const randomIndex = Math.floor(seededRandom(seed + i) * restaurants.length);
20
+ indices.push(randomIndex);
21
  }
22
+
23
+ // Get the corresponding restaurants
24
+ const randomRestaurants = indices.map(index => restaurants[index]);
25
+ return randomRestaurants;
26
  }
27
 
28
  async function displayRestaurants() {
29
  const today = new Date();
30
  const day = today.getDay();
 
 
 
31
 
32
  // Format the date
33
  const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
 
58
  try {
59
  const restaurants = await fetchRestaurants();
60
  console.log('Fetched Restaurants:', restaurants); // Debugging: Print fetched restaurants
61
+ const seed = today.getFullYear() * 10000 + (today.getMonth() + 1) * 100 + today.getDate(); // Create a seed from the date
62
+ const randomRestaurants = getRandomRestaurants(restaurants, seed);
63
  console.log('Random Restaurants:', randomRestaurants); // Debugging: Print random restaurants
64
  document.getElementById('restaurants').innerHTML = randomRestaurants.map(restaurant =>
65
  `<a href="${restaurant[1]}" target="_blank">${restaurant[0]}</a>`